<?php
// +----------------------------------------------------------------------
// | API接口 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------

namespace app\api\controller;
use think\Db;
use Think\Cache;
use app\admin\controller\Config;
use app\common\controller\ApiFornt;

//第三方书城接口
class Thirdnovel extends ApiFornt {

	public function index() {
        echo 'Thirdnovel';
        echo PHP_EOL;
//        $appid = 'rN3TqX';
//        $appsecret = 'rGLikh9YN7iOSX49';
//        $sign  = md5('appid='.$appid.'&'.'appsecret='.$appsecret);
//        echo $sign;
        $bookid = 7127;
        //if(Cache::get('bookinfo'.$bookid)){
		//	$bookinfo = Cache::get('bookinfo'.$bookid);
		//}else{
			$bookinfo = Db::connect("database.db_config_two")->name('novel')->where('id',$bookid)->find();
			//Cache::set('bookinfo'.$bookid,$bookinfo);
			//$bookinfo = [];
		//}
		var_dump($bookinfo);
        exit();

        $order['uid'] = 1;
        $order['spreadid'] = 18055;
        $order['money'] =39.00;
        $order['update_time'] = 1664562450;
        $order['is_income_track'] = 0;
        $order['id'] = 310596;


        file_put_contents(ROOT_PATH."track_novelpay.log",date('Y-m-d H:i:s')."===============开始统计================".PHP_EOL,FILE_APPEND);
        //处理统计开始//
        if(!$order['id']){
            file_put_contents(ROOT_PATH."track_novelpay.log",date('Y-m-d H:i:s')."缺少订单ID".json_encode($order).PHP_EOL,FILE_APPEND);
            return '';
        }
        $orderinfo = Db::connect("database.db_config_two")->name('order')->where(['id' => $order['id']])->find();
        if(!$orderinfo){
            file_put_contents(ROOT_PATH."track_novelpay.log",date('Y-m-d H:i:s')."缺少信息".json_encode($order).PHP_EOL,FILE_APPEND);
        }
        $is_income_track = $orderinfo['is_income_track'];
        if($is_income_track){
            file_put_contents(ROOT_PATH."track_novelpay.log",date('Y-m-d H:i:s')."订单已经统计，请勿重复统计：".$order['id'].PHP_EOL,FILE_APPEND);
            return '';
        }

        if((!$order['uid'])||(!$order['spreadid'])||(!$order['money'])){
            file_put_contents(ROOT_PATH."track_novelpay.log",date('Y-m-d H:i:s')."缺少uid或者spreadid".json_encode($order).PHP_EOL,FILE_APPEND);
            return '';
        }
        $money = (int)$order['money'];
        //注册时间
        $reg_time = '';
        //用户信息
        $resm = Db::connect("database.db_config_two")->name('member')->where('uid','=',$order['uid'])->find();
        if(!$resm){
            file_put_contents(ROOT_PATH."track_novelpay.log",date('Y-m-d H:i:s')."缺少用户信息".json_encode($order).PHP_EOL,FILE_APPEND);
            return '';
        }
        if($resm){
            $reg_time = $resm['reg_time'];  //注册时间戳
            $paytime  = $resm['paytime']; //染色时间
        }
        if(!$reg_time){
            file_put_contents(ROOT_PATH."track_novelpay.log",date('Y-m-d H:i:s')."缺少用户注册时间".json_encode($order).PHP_EOL,FILE_APPEND);
            return '';
        }

        //推广链接信息
        $ress = Db::connect("database.db_config_two")->name('tuiguang')->where('id','=',$order['spreadid'])->find();
        if(!$ress){
            file_put_contents(ROOT_PATH."track_novelpay.log",date('Y-m-d H:i:s')."缺少推广链接信息".json_encode($order).PHP_EOL,FILE_APPEND);
            return '';
        }
        //推广链接用户id
        $tuid = $ress['uid'];
        //推广链接创建时间
        $tui_create_time = $ress['create_time'];
        //转化成推广链接当天凌晨时间
        $tui_create_day = date('Y-m-d',$tui_create_time);
        //推广链接生成凌晨linux时间戳
        $tui_create_time_zero = strtotime($tui_create_day);

        //今天凌晨时间
        $datetody_zero = date('Y-m-d');
        //今天凌晨的时间戳
        $timetody_zero = strtotime($datetody_zero)+1;

        //今天时间减去推广链接创建凌晨时间，算天数
        $times = $timetody_zero - $tui_create_time_zero;
        //判断相差的天数
        $days  =  $times/86400;
        $realdays  = ceil($days);

        //第几天的充值(incomeday)
        $incomeday = $realdays;
        //第几天的时间戳，或对应正式推广时间
        $showstarttime = $tui_create_time_zero + ($realdays-1)*86400;

        //perday处理开始,用户注册的凌晨时间
        $reg_date = date('Y-m-d',$reg_time);
        //用户注册的凌晨时间戳
        $reg_start_time = strtotime($reg_date);
        //今天时间时间戳
        $now_time = time();

        //now_reg_times判断相差的天数时间戳，今天时间减去用户注册注册时间
        $now_reg_times = $now_time - $reg_start_time;
        $income_old = 0;
        $income_new = 0;
        //var_dump($now_reg_times);
        if($now_reg_times<86400){
            $income_new = $money;
        }else{
            $income_old = $money;
        }

        //推广链接第一天记录是否存在
        $res = Db::connect("database.db_config_two")->name('tuiguang_count')->where('tid','=',$order['spreadid'])->where('incomeday','=',$incomeday)->find();
        if(!$res){
            $tdata['tid'] = $order['spreadid'];
            $tdata['incomeday'] = $incomeday;
            $tdata['showstarttime'] = $showstarttime;
            $tdata['create_time'] = date('Y-m-d H:i:s',time());
            $tdata['update_time'] = date('Y-m-d H:i:s',time());
            $tdata['tuiguang_create_time'] = $tui_create_time;
            $tdata['income'] = $money;
            $tdata['income_old'] = $income_old;
            $tdata['income_new'] = $income_new;
            $tdata['uid'] = $tuid;
            $re = Db::connect("database.db_config_two")->name('tuiguang_count')->insertGetId($tdata);
            file_put_contents(ROOT_PATH."track_novelpay.log",date('Y-m-d H:i:s')."统计完毕-新增：".$re.PHP_EOL,FILE_APPEND);

        }else{
            if($order['is_income_track']!=1){
                //修改记录
                $tdata['income'] = $res['income'] + $money;
                $tdata['income_old'] = $res['income_old'] + $income_old;
                $tdata['income_new'] = $res['income_new'] + $income_new;
                $tdata['update_time'] = date('Y-m-d H:i:s',time());
                if(!$res['uid']){$tdata['uid'] = $tuid;}
                $re = Db::connect("database.db_config_two")->name('tuiguang_count')->where('tid','=',$order['spreadid'])->where('incomeday','=',$incomeday)->update($tdata);
                file_put_contents(ROOT_PATH."track_novelpay.log",date('Y-m-d H:i:s')."统计完毕-修改：".$re.PHP_EOL,FILE_APPEND);
            }
                //return $re;
        }
        //var_dump($re);
        if($re){
            $r = Db::connect("database.db_config_two")->name('order')->where(['id' => $order['id']])->update(['is_income_track' => 1,'user_reg_time'=>$reg_time]);
            file_put_contents(ROOT_PATH."track_novelpay.log",date('Y-m-d H:i:s')."修改订单：".$r.PHP_EOL,FILE_APPEND);
        }
        file_put_contents(ROOT_PATH."track_novelpay.log",date('Y-m-d H:i:s')."===============结束统计================统计回传值结果：".$re.PHP_EOL,FILE_APPEND);
        //处理统计结束//


        exit();
        if($order['uid']){
            $resm = Db::connect("database.db_config_two")->name('member')->where('uid','=',$order['uid'])->find();
            //var_dump($res);
            if($resm){
                $reg_time = $resm['reg_time'];  //注册时间戳
                $spreadid = $resm['spreadid'];  //推广链接id
                var_dump($reg_time);
            }

            if($spreadid){
                $ress = Db::connect("database.db_config_two")->name('tuiguang')->where('id','=',$spreadid)->find();
                $create_time = $ress['create_time'];
                var_dump($create_time);
                $datetody = date('Y-m-d');
                $timetody = strtotime($datetody);

                var_dump($timetody);
                //是否存在今天的记录，不存在则插入一条
//                $res = Db::connect("database.db_config_two")->name('tuiguang_count')->where('tid','=',$spreadid)->where('showstarttime','=',$timetody)->find();
//                if(!$res){
//                    $tdata['tid'] = $spreadid;
//                    $tdata['showstarttime'] = $timetody;
//                    Db::connect("database.db_config_two")->name('tuiguang_count')->insertGetId($tdata);
//                }

            }

            //用户注册时间先于绑定的链接创建时间，算第一天的用户
            if($reg_time<$create_time){
                //用户是否存在染色时间，染色时间，不存在更新染色时间到当天，并算当天的用户

                echo '先';
            }elseif($reg_time>$create_time){
                //转化成推广链接当天凌晨时间
                $create_day = date('Y-m-d',$create_time);
                $create_timez = strtotime($create_day);
                $times = $reg_time - $create_timez;
                //判断相差的天数
                $days  =  $times/86400;

                $realdays  = ceil($days);
                $incomeday = $realdays;
                //相差一天为当天用户，其余31天对应第31天用户，32天对应第二个月，33天对应第三个月，34天以上为四个月以上的用户
                echo '天'.$realdays;
                echo '后';

                if($realdays<=1){
                    //插入第一天新用户消耗的记录之中
                    //$incomeday = 1;
                    $perday = 'income_'.$realdays;
                }elseif(($realdays>1)&&($realdays<=31)){
                    //$incomeday = $realdays;
                    $perday = 'income_'.$realdays;

                }elseif(($realdays>31)&&($realdays<=60)){
                    $perday = 'income_32';
                    $incomeday = 60;
                }elseif(($realdays>60)&&($realdays<=90)){
                    $perday = 'income_33';
                    $incomeday = 90;
                }else{
                    $perday = 'income_34';
                    $incomeday = 999;
                }
                //$tdata[$perday] = 100;
                //推广链接第一天记录是否存在
                $res = Db::connect("database.db_config_two")->name('tuiguang_count')->where('tid','=',$spreadid)->where('incomeday','=',$incomeday)->find();
                if(!$res){
                    $tdata['tid'] = $spreadid;
                    $tdata['incomeday'] = $incomeday;
                    Db::connect("database.db_config_two")->name('tuiguang_count')->insertGetId($tdata);
                }else{
                    //修改记录
                    $money = $res[$perday] + 100;
                    $tdata[$perday]  = $money;
                    $res = Db::connect("database.db_config_two")->name('tuiguang_count')->where('tid','=',$spreadid)->where('incomeday','=',$incomeday)->update($tdata);
                    var_dump($res);
                }

            }

        }

	}

    //书籍列表（鼎甜）
    public function listdingtian() {
        $header= array("content-type: application/json");
        $url = 'http://api.sweetread.cn/xml/index/?method=booklist&sign=08767810D3AA4CB3ADC628644AAB4C67';

        //获取列表
        $result = $this->curl_get($url,$header);

        if(($result['code']==1)&&($result['data']['books'])){
            $data = $result['data']['books'];
            $siteid = 8;

            //循环插入数据库表
            $dataIn['siteid'] = $siteid;
            foreach ($data as $key=>$value){
                //处理返回最后更新时间
                //$tirdtimestr = $value['lastupdate'];
                //$tirdtime = strtotime($tirdtimestr);

                //判断是否存在
                $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$value['id'])->find();
                if ($res !== null) {
                    $dataIn['third_id'] = $value['id'];
                    $dataIn['third_title'] = '鼎甜-'.$value['id'];
                    //$dataIn['third_time'] = $tirdtime;
                    //$dataIn['third_time_str'] = $tirdtimestr;
                    $dataIn['update_time'] = time();
                    $re = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$value['id'])->update($dataIn);
                    echo '鼎甜：ID'.$res['id'].'-'.$re.'更新成功'.PHP_EOL;
                }else{
                    $dataIn['third_id'] = $value['id'];
                    $dataIn['third_title'] = '鼎甜-'.$value['id'];;
                    //$dataIn['third_time'] = $tirdtime;
                    //$dataIn['third_time_str'] = $tirdtimestr;
                    $dataIn['create_time'] = date('Y-m-d h:i:s',time());
                    $re = Db::connect("database.db_config_two")->name('third_booklist')->insertGetId($dataIn);
                    echo '鼎甜：'.$re.'增加成功'.PHP_EOL;
                }

            }

        }else{
            return '鼎甜-书籍列表未获取到';
        }

    }


    //书籍列表-详情（鼎甜）
    public function dingtianovel() {
        $field = ['id','third_id','third_title','siteid'];
        $siteid = 8;
        $list = Db::connect("database.db_config_two")->name('third_booklist')->field($field)->where('siteid','=',$siteid)->chunk(10,function ($datas) {
            foreach ($datas as $data){

                $header= array("content-type: application/json");
                $url = 'http://api.sweetread.cn/xml/index/?method=bookinfo&sign=08767810D3AA4CB3ADC628644AAB4C67&bid='.$data['third_id'];
                //获取列表
                $getdata = $this->curl_get($url,$header);

                $result ='';
                if(($getdata['code']==1)&&($getdata['data'])){
                    $result =$getdata['data'];
                }

                if($result){

                    //处理分类id
//                    $category_id = $result['sortid'];
//                    $category_id = $result['sortid'];
//                    //频道
//                    $channel = 1;
//                    $cateinfo = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$data['siteid'])->where('site_cid','=',$result['sortid'])->find();
//                    if($cateinfo['real_cid']){
//                        $category_id = $cateinfo['real_cid'];
//                    }
//                    if($cateinfo['name']=='女频'){
//                        $channel = 2;
//                    }

                    //小说名字
                    $dataIn['uid'] = 1;
                    $dataIn['title'] = $result['name'];
                    $dataIn['fullflag'] = 1; //小说进程，暂不确定
                    $dataIn['category_id'] = 1; //分类id
                    $dataIn['siteid'] = $data['siteid']; //内容源id
                    $dataIn['sourceid'] = $result['id']; //内容源内容id
                    $dataIn['intro']  = $result['introduce']; //简介
                    //$dataIn['size'] = $result['words']; //字数
                    $dataIn['author'] = $result['author']; //作者
                    $dataIn['isvip'] = 1; //是否VIP上架 0 :否  1:是
                    //$dataIn['batchbuy'] = $result['isFull']; //是否整本购买
                    //$dataIn['batchprice'] = $result['all_price']; //batchprice
                    $dataIn['coverl'] = $result['icon']; //封页图
                    $dataIn['channel'] = 2; //性别频道
                    //小说最新更新时间？
                    $dataIn['update_time'] = strtotime($result['updateTime']); //小说最新更新时间

                    //判断是否存在
                    $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['id'])->find();
                    if ($res['nid'] == 0) {
                        $dataIn['create_time'] = time(); //创建时间
                        $re = Db::connect("database.db_config_two")->name('third_novel')->insertGetId($dataIn);
                        $datalist['nid'] = $re;
                        Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['id'])->update($datalist);
                        echo '鼎甜novel：ID'.$res['id'].'-'.$re.'创建成功'.PHP_EOL;
                    }else{
                        $dataIn['update_time'] = time(); //创建时间
                        $re = Db::connect("database.db_config_two")->name('third_novel')->where('siteid','=',$data['siteid'])->where('id','=',$res['nid'])->update($dataIn);
                        echo '鼎甜novel：'.$re.'修改成功'.PHP_EOL;
                    }

                }
            }
        });

    }


    //章节章节列表-鼎甜
    public function dingtianchapters() {
        //获取所有参数
        $request = request();
        //第三站定id
        $siteid = $request->param('siteid');
        //第三方文章id
        $third_id = $request->param('third_id');
        if(!$siteid){$siteid = 8;}

        if($third_id){
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('third_id','=',$third_id)
                ->find();
        }else{
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('status','=',0)
                ->find();
        }

        //不存在返回空
        if(!$result){
            return null;
        }

        //调用接口请求书籍章节列表
        $header= array("content-type: application/json");
        $url = 'http://api.sweetread.cn/xml/index/?method=chapterlist&sign=08767810D3AA4CB3ADC628644AAB4C67&bid='.$result['third_id'];
        $res = $this->curl_get($url,$header);

        //不存在返回空
        if(!$res){return null;}
        if(($res['code']!=1)||(!$res['data']['volumes']['0']['chapters'])){return null;}
        $datas = $res['data']['volumes']['0']['chapters'];


        //更新书籍章节数量
        $chapters = count($datas);
        $dataUp['chapters'] = $chapters;
        Db::connect("database.db_config_two")->name('third_novel')->where('id','=',$result['id'])->update($dataUp);
        //更新书籍章节数量

        $dataIn['siteid'] = $result['siteid'];
        $dataIn['sourceid'] = $result['third_id'];
        $dataIn['status'] = 2;
        $dataIn['uid'] = 1;
        $dataIn['articleid'] = $result['id'];

        foreach ($datas as $key=>$value){
            $dataIn['sourcecid'] = $value['chapterId'];   //源章节id
            $dataIn['chaptername'] = '第'.$value['chapterNumber'].'章';   //章节名字
            $dataIn['chapterorder'] = $value['chapterNumber'];  //章节序号
//            $dataIn['size'] = $value['words'];
//            $dataIn['isvip'] = $value['isvip'];
//            $dataIn['saleprice'] = $value['saleprice'];
//            //创建时间
//            $dataIn['create_time'] = strtotime($value['postdate']);
//            //更新时间
//            $dataIn['update_time'] = strtotime($value['lastupdate']);

            $re = Db::connect("database.db_config_two")->name('third_chapter')
                ->where('siteid','=',$result['siteid'])
                ->where('sourceid','=',$result['third_id'])
                ->where('sourcecid','=',$value['chapterId'])
                ->find();

            if($re){
                $r = Db::connect("database.db_config_two")->name('third_chapter')
                    ->where('siteid','=',$result['siteid'])
                    ->where('sourceid','=',$result['third_id'])
                    ->where('sourcecid','=',$value['chapterId'])
                    ->update($dataIn);
                echo '鼎甜third_chapter：'.$r.'修改成功'.PHP_EOL;
            }else{
                $rid = Db::connect("database.db_config_two")->name('third_chapter')->insertGetId($dataIn);
                //更新文章排序
                if($rid){
                    Db::connect("database.db_config_two")->name('third_chapter')->where('id','=',$rid)->update($dataUp);
                }
                echo '鼎甜third_chapter：'.$rid.'增加成功'.PHP_EOL;
            }

        }

    }


    //鼎甜书籍章节内容
    public function diantiancontent() {
        //获取所有参数
        $request = request();
        //第三站定id
        $siteid = $request->param('siteid');
        //第三方文章id
        $third_id = $request->param('third_id');
        if(!$siteid){$siteid = 8;}

        if($third_id){
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('third_id','=',$third_id)
                ->find();
        }else{
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('status','=',0)
                ->find();
        }

        if(!$result){
            return null;
        }
        $header= array("content-type: application/json");

        //书籍章节
        $chapters = Db::connect("database.db_config_two")->name('third_chapter')
            ->where('siteid','=',$result['siteid'])
            ->where('sourceid','=',$result['third_id'])
            ->chunk(100,function ($datas) use($header) {
                foreach ($datas as $data){
                    //获取书籍内容

                    $url = 'http://api.sweetread.cn/xml/index/?method=chapter&sign=08767810D3AA4CB3ADC628644AAB4C67&bid='.$data['sourceid'].'&cid='.$data['sourcecid'];
//                    var_dump($url);
//                    exit();
                    $res = $this->curl_get($url,$header);

                    //存在文章内容则插入
                    if(($res['code']==1)&&($res['data'])){
                        $content = $res['data']['content'];
                        //获取文章内容
                        $dataIn['content'] =  $content;
                        $re = Db::connect("database.db_config_two")->name('third_novel_content')->where('chapterid','=',$data['id'])->find();
                        if($re){
                            $r = Db::connect("database.db_config_two")->name('third_novel_content')
                                ->where('chapterid','=',$data['id'])
                                ->update($dataIn);
                            echo '鼎甜third_novel_content：'.$re['id'].'修改成功'.PHP_EOL;
                        }else{
                            $dataIn['chapterid'] = $data['id'];
                            $r = Db::connect("database.db_config_two")->name('third_novel_content')->insertGetId($dataIn);
                            echo '鼎甜third_novel_content：'.$r.'增加成功'.PHP_EOL;
                        }

                        if($res['data']['name']){
                            $datachapter['chaptername'] = $res['data']['name'];
                            $r = Db::connect("database.db_config_two")->name('third_chapter')
                                ->where('id','=',$data['id'])
                                ->update($datachapter);
                            if($r){
                                echo '鼎甜third_chapter：'.$re['id'].'修改成功'.PHP_EOL;
                            }else{
                                echo '鼎甜third_chapter：'.$re['id'].'修改成功'.PHP_EOL;
                            }
                        }

                    }
                }
            });
        var_dump($chapters);
    }




    //书籍列表（依米）
    public function cateyimi() {
        $header= array("content-type: application/json");

        $appid = 'rN3TqX';
        $appsecret = 'rGLikh9YN7iOSX49';
        $sign  = md5('appid='.$appid.'&appsecret='.$appsecret);
        $url = 'http://api.qiuqiuart.com/share/category?appid='.$appid.'&sign='.$sign;
        //获取列表
        $result = $this->curl_get($url,$header);
        //var_dump($result);
        //exit();
        if(($result['code']==1)&&($result['data'])) {
            $siteid = 7;
            //循环插入数据库表
            $dataIn['siteid'] = $siteid;

            $data = $result['data'];
            foreach ($data as $key=>$value){
                $name = '女频';
                if($value['channelid']==1){$name = '男频';}

                //判断是否存在分类
                $res = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$siteid)->where('site_cid','=',$value['sortid'])->find();
                if($res !=null){
                    $dataIn['name'] = $name;
                    $dataIn['title'] = $value['sortname'];
                    $dataIn['site_cid'] = $value['sortid'];
                    $dataIn['siteid'] = $siteid;
                    $re = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$siteid)->where('site_cid','=',$value['sortid'])->update($dataIn);
                    echo '依米：ID'.$res['id'].'-'.$re.'更新成功'.PHP_EOL;
                }else{
                    $dataIn['name'] = $name;
                    $dataIn['title'] = $value['sortname'];
                    $dataIn['site_cid'] = $value['sortid'];
                    $dataIn['siteid'] = $siteid;
                    $re = Db::connect("database.db_config_two")->name('third_category')->insertGetId($dataIn);
                    echo '依米：'.$re.'增加成功'.PHP_EOL;
                }

            }
        }else{
            return '';
        }

    }

    //书籍列表（依米）
    public function listyimi() {
        $header= array("content-type: application/json");

        $appid = 'rN3TqX';
        $appsecret = 'rGLikh9YN7iOSX49';
        $sign  = md5('appid='.$appid.'&'.'appsecret='.$appsecret);
        $url = 'http://api.qiuqiuart.com/share/booklist?appid='.$appid.'&sign='.$sign;

        //获取列表
        $result = $this->curl_get($url,$header);
        if(($result['code']==1)&&($result['data'])){
            $data = $result['data'];
            $siteid = 7;
            //循环插入数据库表
            $dataIn['siteid'] = $siteid;
            foreach ($data as $key=>$value){
                //处理返回最后更新时间
                $tirdtimestr = $value['lastupdate'];
                $tirdtime = strtotime($tirdtimestr);

                //判断是否存在
                $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$value['articleid'])->find();
                if ($res !== null) {
                    $dataIn['third_id'] = $value['articleid'];
                    $dataIn['third_title'] = $value['articlename'];
                    $dataIn['third_time'] = $tirdtime;
                    $dataIn['third_time_str'] = $tirdtimestr;
                    $dataIn['update_time'] = time();
                    $re = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$value['articleid'])->update($dataIn);
                    echo '依米：ID'.$res['id'].'-'.$re.'更新成功'.PHP_EOL;
                }else{
                    $dataIn['third_id'] = $value['articleid'];
                    $dataIn['third_title'] = $value['articlename'];
                    $dataIn['third_time'] = $tirdtime;
                    $dataIn['third_time_str'] = $tirdtimestr;
                    $dataIn['create_time'] = date('Y-m-d h:i:s',time());
                    $re = Db::connect("database.db_config_two")->name('third_booklist')->insertGetId($dataIn);
                    echo '依米：'.$re.'增加成功'.PHP_EOL;
                }
            }
        }else{
            return '书籍列表未获取到';
        }

    }

    //书籍列表-详情（依米）
    public function yiminovel() {
        $field = ['id','third_id','third_title','siteid'];
        $siteid = 7;
        $list = Db::connect("database.db_config_two")->name('third_booklist')->field($field)->where('siteid','=',$siteid)->chunk(10,function ($datas) {
            foreach ($datas as $data){
                //$result = $this->bookbanke($data['third_id']);
                $appid = 'rN3TqX';
                $appsecret = 'rGLikh9YN7iOSX49';
                $sign  = md5('appid='.$appid.'&appsecret='.$appsecret.'&bookid='.$data['third_id']);
                $url = 'http://api.qiuqiuart.com/share/bookinfo?appid=rN3TqX&sign='.$sign.'&bookid='.$data['third_id'];
                $header= array("content-type: application/json");
                $getdata = $this->curl_get($url,$header);
                $result ='';
                if(($getdata['code']==1)&&($getdata['data'])){
                    $result =$getdata['data'];
                }

                if($result){

                    //处理分类id
                    $category_id = $result['sortid'];
                    //频道
                    $channel = 1;
                    $cateinfo = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$data['siteid'])->where('site_cid','=',$result['sortid'])->find();
                    if($cateinfo['real_cid']){
                        $category_id = $cateinfo['real_cid'];
                    }
                    if($cateinfo['name']=='女频'){
                        $channel = 2;
                    }

                    //小说名字
                    $dataIn['uid'] = 1;
                    $dataIn['title'] = $result['articlename'];
                    $dataIn['fullflag'] = 1; //小说进程，暂不确定
                    $dataIn['category_id'] = $category_id; //分类id
                    $dataIn['siteid'] = $data['siteid']; //内容源id
                    $dataIn['sourceid'] = $result['articleid']; //内容源内容id
                    $dataIn['intro']  = $result['intro']; //简介
                    $dataIn['size'] = $result['words']; //字数
                    $dataIn['author'] = $result['author']; //作者
                    $dataIn['isvip'] = 1; //是否VIP上架 0 :否  1:是
                    //$dataIn['batchbuy'] = $result['isFull']; //是否整本购买
                    //$dataIn['batchprice'] = $result['all_price']; //batchprice
                    $dataIn['coverl'] = $result['cover']; //封页图
                    $dataIn['channel'] = $channel; //性别频道
                    //小说最新更新时间？
                    $dataIn['update_time'] = strtotime($result['lastupdate']); //小说最新更新时间

                    //判断是否存在
                    $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['articleid'])->find();
                    if ($res['nid'] == 0) {
                        $dataIn['create_time'] = time(); //创建时间
                        $re = Db::connect("database.db_config_two")->name('third_novel')->insertGetId($dataIn);
                        $datalist['nid'] = $re;
                        Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['articleid'])->update($datalist);
                        echo '依米novel：ID'.$res['id'].'-'.$re.'创建成功'.PHP_EOL;
                    }else{
                        $dataIn['update_time'] = time(); //创建时间
                        $re = Db::connect("database.db_config_two")->name('third_novel')->where('siteid','=',$data['siteid'])->where('id','=',$res['nid'])->update($dataIn);
                        echo '依米novel：'.$re.'修改成功'.PHP_EOL;
                    }

                }
            }
        });

    }


    //章节章节列表-依米
    public function yimichapters() {
        //获取所有参数
        $request = request();
        //第三站定id
        $siteid = $request->param('siteid');
        //第三方文章id
        $third_id = $request->param('third_id');
        if(!$siteid){$siteid = 7;}

        if($third_id){
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('third_id','=',$third_id)
                ->find();
        }else{
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('status','=',0)
                ->find();
        }

        //不存在返回空
        if(!$result){
            return null;
        }

        //调用接口请求书籍章节列表
        $header= array("content-type: application/json");
        $appid = 'rN3TqX';
        $appsecret = 'rGLikh9YN7iOSX49';
        $sign  = md5('appid='.$appid.'&appsecret='.$appsecret.'&bookid='.$result['third_id']);
        $url = 'http://api.qiuqiuart.com/share/chapterlist?appid=rN3TqX&sign='.$sign.'&bookid='.$result['third_id'];
        $res = $this->curl_get($url,$header);

        //不存在返回空
        if(!$res){return null;}
        if(($res['code']!=1)||(!$res['data'])){return null;}
        $datas = $res['data'];

        //更新书籍章节数量
        $chapters = count($datas);
        $dataUp['chapters'] = $chapters;
        Db::connect("database.db_config_two")->name('third_novel')->where('id','=',$result['id'])->update($dataUp);
        //更新书籍章节数量

        $dataIn['siteid'] = $result['siteid'];
        $dataIn['sourceid'] = $result['third_id'];
        $dataIn['status'] = 2;
        $dataIn['uid'] = 1;
        $dataIn['articleid'] = $result['id'];

        foreach ($datas as $key=>$value){
            $dataIn['sourcecid'] = $value['chapterid'];
            $dataIn['chaptername'] = $value['chaptername'];
            $dataIn['chapterorder'] = $value['chapterorder'];
            $dataIn['size'] = $value['words'];
            $dataIn['isvip'] = $value['isvip'];
            $dataIn['saleprice'] = $value['saleprice'];
            //创建时间
            $dataIn['create_time'] = strtotime($value['postdate']);
            //更新时间
            $dataIn['update_time'] = strtotime($value['lastupdate']);

            $re = Db::connect("database.db_config_two")->name('third_chapter')
                ->where('siteid','=',$result['siteid'])
                ->where('sourceid','=',$result['third_id'])
                ->where('sourcecid','=',$value['chapterid'])
                ->find();

            if($re){
                $r = Db::connect("database.db_config_two")->name('third_chapter')
                    ->where('siteid','=',$result['siteid'])
                    ->where('sourceid','=',$result['third_id'])
                    ->where('sourcecid','=',$value['chapterid'])
                    ->update($dataIn);
                echo '依米third_chapter：'.$r.'修改成功'.PHP_EOL;
            }else{
                $rid = Db::connect("database.db_config_two")->name('third_chapter')->insertGetId($dataIn);
                //更新文章排序
                if($rid){
                    Db::connect("database.db_config_two")->name('third_chapter')->where('id','=',$rid)->update($dataUp);
                }
                echo '依米third_chapter：'.$rid.'增加成功'.PHP_EOL;
            }

        }

    }


    //依米书籍章节内容
    public function yimicontent() {
        //获取所有参数
        $request = request();
        //第三站定id
        $siteid = $request->param('siteid');
        //第三方文章id
        $third_id = $request->param('third_id');
        if(!$siteid){$siteid = 7;}

        if($third_id){
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('third_id','=',$third_id)
                ->find();
        }else{
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('status','=',0)
                ->find();
        }

        if(!$result){
            return null;
        }

        //调用接口请求书籍章节列表
        $header= array("content-type: application/json");
        $appid = 'rN3TqX';
        $appsecret = 'rGLikh9YN7iOSX49';

        //书籍章节
        $chapters = Db::connect("database.db_config_two")->name('third_chapter')
            ->where('siteid','=',$result['siteid'])
            ->where('sourceid','=',$result['third_id'])
            ->chunk(100,function ($datas) use($appid,$appsecret,$header) {
                foreach ($datas as $data){
                    //获取书籍内容
                    $sign  = md5('appid='.$appid.'&appsecret='.$appsecret.'&bookid='.$data['sourceid'].'&chapterid='.$data['sourcecid']);
                    $url = 'http://api.qiuqiuart.com/share/chaptercontent?appid=rN3TqX&sign='.$sign.'&bookid='.$data['sourceid'].'&chapterid='.$data['sourcecid'];
                    $res = $this->curl_get($url,$header);

                    //存在文章内容则插入
                    if(($res['code']==1)&&($res['data'])){
                        $content = $res['data']['content'];
                        //获取文章内容
                        $dataIn['content'] =  $content;
                        $re = Db::connect("database.db_config_two")->name('third_novel_content')->where('chapterid','=',$data['id'])->find();
                        if($re){
                            $r = Db::connect("database.db_config_two")->name('third_novel_content')
                                ->where('chapterid','=',$data['id'])
                                ->update($dataIn);
                            echo '依米third_novel_content：'.$re['id'].'修改成功'.PHP_EOL;
                        }else{
                            $dataIn['chapterid'] = $data['id'];
                            $r = Db::connect("database.db_config_two")->name('third_novel_content')->insertGetId($dataIn);
                            echo '依米third_novel_content：'.$r.'增加成功'.PHP_EOL;
                        }
                    }
                }
            });
        var_dump($chapters);
    }




    //书籍列表（半刻）
    public function listbanke() {
	    $url = 'http://www.bankexs.com/api/share/get_book_list.php?ssid=78';
	    //$ssid = 78; //渠道标识，半刻分配的
        $header= array("content-type: application/json");
        $result = $this->curl_get($url,$header);
        if(($result['code']==200)&&($result['data'])){
            $data = $result['data'];
            $siteid = 6;
            //循环插入数据库表
            $dataIn['siteid'] = $siteid;
            foreach ($data as $key=>$value){
                //处理半刻返回时间
                $strlen = strlen($value['lastupdate']);
                if($strlen==14){
                    $tirdtimestr = substr($value['lastupdate'],0,4).'-'.substr($value['lastupdate'],4,2).'-'.substr($value['lastupdate'],6,2).' '.substr($value['lastupdate'],8,2).':'.substr($value['lastupdate'],10,2).':'.substr($value['lastupdate'],12,2);
                    $tirdtime = strtotime($tirdtimestr);
                }else{
                    $tirdtime = time();
                    $tirdtimestr  = date('Y-m-d h:i:s');
                }

                //判断是否存在
                $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$value['articleid'])->find();
                if ($res !== null) {
                    $dataIn['third_id'] = $value['articleid'];
                    $dataIn['third_title'] = $value['articlename'];
                    $dataIn['third_time'] = $tirdtime;
                    $dataIn['third_time_str'] = $tirdtimestr;
                    $dataIn['update_time'] = time();
                    $re = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$value['articleid'])->update($dataIn);
                    echo '半刻：ID'.$res['id'].'-'.$re.'更新成功'.PHP_EOL;
                }else{
                    $dataIn['third_id'] = $value['articleid'];
                    $dataIn['third_title'] = $value['articlename'];
                    $dataIn['third_time'] = $tirdtime;
                    $dataIn['third_time_str'] = $tirdtimestr;
                    $dataIn['create_time'] = date('Y-m-d h:i:s',time());
                    $re = Db::connect("database.db_config_two")->name('third_booklist')->insertGetId($dataIn);
                    echo '半刻：'.$re.'增加成功'.PHP_EOL;
                }
            }
        }else{
            return '书籍列表未获取到';
        }

    }

    //书籍列表（半刻）
    public function bankenovel() {
        $field = ['id','third_id','third_title','siteid'];
        $siteid = 6;
        $list = Db::connect("database.db_config_two")->name('third_booklist')->field($field)->where('siteid','=',$siteid)->chunk(10,function ($datas) {
            foreach ($datas as $data){
                $result = $this->bookbanke($data['third_id']);
                if($result){

                    //处理分类id
                    $category_id = $result['sortid'];
                    //频道
                    $channel = 1;
                    $cateinfo = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$data['siteid'])->where('site_cid','=',$result['sortid'])->find();
                    if($cateinfo['real_cid']){
                        $category_id = $cateinfo['real_cid'];
                    }
                    if($cateinfo['name']=='女频'){
                        $channel = 2;
                    }

                    //小说名字
                    $dataIn['uid'] = 1;
                    $dataIn['title'] = $result['articlename'];
                    $dataIn['fullflag'] = 1; //小说进程，暂不确定
                    $dataIn['category_id'] = $category_id; //分类id
                    $dataIn['siteid'] = $data['siteid']; //内容源id
                    $dataIn['sourceid'] = $result['articleid']; //内容源内容id
                    $dataIn['intro']  = $result['intro']; //简介
                    $dataIn['size'] = $result['words']; //字数
                    $dataIn['author'] = $result['author']; //作者
                    $dataIn['isvip'] = $result['isvip']; //是否VIP上架 0 :否  1:是
                    //$dataIn['batchbuy'] = $result['isFull']; //是否整本购买
                    //$dataIn['batchprice'] = $result['all_price']; //batchprice
                    $dataIn['coverl'] = $result['cover']; //封页图
                    $dataIn['channel'] = $channel; //性别频道
                    //处理时间
                    $returntime = $this->bankestrtotime($result['lastupdate']);
                    $dataIn['update_time'] = $returntime['time']; //更新时间

                    //判断是否存在
                    $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['articleid'])->find();
                    if ($res['nid'] == 0) {
                        $dataIn['create_time'] = time(); //创建时间
                        $re = Db::connect("database.db_config_two")->name('third_novel')->insertGetId($dataIn);
                        $datalist['nid'] = $re;
                        Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['articleid'])->update($datalist);
                        echo '半刻novel：ID'.$res['id'].'-'.$re.'创建成功'.PHP_EOL;
                    }else{
                        $dataIn['update_time'] = time(); //创建时间
                        $re = Db::connect("database.db_config_two")->name('third_novel')->where('siteid','=',$data['siteid'])->where('id','=',$res['nid'])->update($dataIn);
                        echo '半刻novel：'.$re.'修改成功'.PHP_EOL;
                    }

                }
            }
        });

    }

    //半刻章节章节列表
    public function bankechapters() {
        //获取所有参数
        $request = request();
        //第三站定id
        $siteid = $request->param('siteid');
        //第三方文章id
        $third_id = $request->param('third_id');
        if(!$siteid){$siteid = 6;}

        if($third_id){
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('third_id','=',$third_id)
                ->find();
        }else{
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('status','=',0)
                ->find();
        }

        if(!$result){
            return null;
        }
        $url = 'http://www.bankexs.com/api/share/get_chapter_list.php?ssid=78&aid='.$result['third_id'];
        //$ssid = 78; //渠道标识，半刻分配的
        $header= array("content-type: application/json");
        $res = $this->curl_get($url,$header);

        if(!$res){return null;}
        if(($res['code']!=200)||(!$res['data'])){return null;}
        $datas = $res['data'];

        //更新书籍章节数量
        $chapters = count($datas);
        $dataUp['chapters'] = $chapters;
        Db::connect("database.db_config_two")->name('third_novel')->where('id','=',$result['id'])->update($dataUp);
        //更新书籍章节数量

        $dataIn['siteid'] = $result['siteid'];
        $dataIn['sourceid'] = $result['third_id'];
        $dataIn['status'] = 2;
        $dataIn['uid'] = 1;
        $dataIn['articleid'] = $result['id'];

        foreach ($datas as $key=>$value){
            $dataIn['sourcecid'] = $value['chapterid'];
            $dataIn['chaptername'] = $value['chaptername'];
            $dataIn['size'] = $value['words'];
            $dataIn['isvip'] = $value['isvip'];
            $dataIn['saleprice'] = $value['saleprice'];
            $postreturn = $this->bankestrtotime($value['postdate']);
            //创建时间
            $create_time = $postreturn['time'];
            $dataIn['create_time'] = $create_time;
            //更新时间
            $lastreturn = $this->bankestrtotime($value['lastupdate']);
            $update_time = $lastreturn['time'];
            $dataIn['update_time'] = $update_time;

            $re = Db::connect("database.db_config_two")->name('third_chapter')
                ->where('siteid','=',$result['siteid'])
                ->where('sourceid','=',$result['third_id'])
                ->where('sourcecid','=',$value['chapterid'])
                ->find();

            if($re){
                $r = Db::connect("database.db_config_two")->name('third_chapter')
                    ->where('siteid','=',$result['siteid'])
                    ->where('sourceid','=',$result['third_id'])
                    ->where('sourcecid','=',$value['chapterid'])
                    ->update($dataIn);
                echo '半刻third_chapter：'.$r.'修改成功'.PHP_EOL;
            }else{
                $rid = Db::connect("database.db_config_two")->name('third_chapter')->insertGetId($dataIn);
                //更新文章排序
                if($rid){
                    $dataUp['chapterorder'] = $rid;
                    Db::connect("database.db_config_two")->name('third_chapter')->where('id','=',$rid)->update($dataUp);
                }
                echo '半刻third_chapter：'.$rid.'增加成功'.PHP_EOL;
            }

        }

    }



    //半夜书籍章节内容
    public function bankecontent() {
	    //获取所有参数
        $request = request();
        //第三站定id
        $siteid = $request->param('siteid');
        //第三方文章id
        $third_id = $request->param('third_id');
        if(!$siteid){$siteid = 6;}

        if($third_id){
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('third_id','=',$third_id)
                ->find();
        }else{
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('status','=',0)
                ->find();
        }

        if(!$result){
            return null;
        }

        //书籍id
        $bookId = $result['third_id'];
        //$ssid = 78; //渠道标识，半刻分配的
        $url = 'http://www.bankexs.com/api/share/get_chapter_content.php?ssid=78&aid='.$bookId.'&cid=';
        $header= array("content-type: application/json");

        //书籍章节
        $chapters = Db::connect("database.db_config_two")->name('third_chapter')
            ->where('siteid','=',$result['siteid'])
            ->where('sourceid','=',$result['third_id'])
            ->chunk(100,function ($datas) use($url,$header) {
                foreach ($datas as $data){
                    $datasend['id'] = $data['sourcecid'];
                    $sendurl = $url.$data['sourcecid'];
                    $res = $this->curl_get($sendurl,$header);
                    //存在文章内容则插入
                    if(($res['code']==200)&&($res['data'])){
                        $content = $res['data']['content'];
                        //获取文章内容
                        $dataIn['content'] =  $content;
                        $re = Db::connect("database.db_config_two")->name('third_novel_content')->where('chapterid','=',$data['id'])->find();
                        if($re){
                            $r = Db::connect("database.db_config_two")->name('third_novel_content')
                                ->where('chapterid','=',$data['id'])
                                ->update($dataIn);
                            echo '半刻third_novel_content：'.$re['id'].'修改成功'.PHP_EOL;
                        }else{
                            $dataIn['chapterid'] = $data['id'];
                            $r = Db::connect("database.db_config_two")->name('third_novel_content')->insertGetId($dataIn);
                            echo '半刻third_novel_content：'.$r.'增加成功'.PHP_EOL;
                        }
                    }
                }
            });
        var_dump($chapters);
    }

    //书籍列表（九库）
    public function booklist() {
        //获取所有参数
        $pid = 218;
        $key ='299cc15361c629ec43d36380326dedc1';
        $sn  = md5('pid=218&key=299cc15361c629ec43d36380326dedc1');
        $url = 'http://Inf.9kus.com/communalData/bookList/';

        $data = array(
            "returnType" => "JSON",
            "pid" => $pid,
            "sn" => $sn
        );
        $header= array(
            //"content-type: application/json",
            //"x-signature: " . $hash
        );
        $result = $this->curl_posts($url,$data,$header);
        if(($result['status']!=1)||(!$result['data'])){
            return '数据不存在';
        }
        $data = $result['data'];
        //循环插入数据库表
        $dataIn['siteid'] = 5;
        foreach ($data as $key=>$value){
            //判断是否存在
            $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',5)->where('third_id','=',$value['id'])->find();
            if ($res !== null) {
                $dataIn['third_id'] = $value['id'];
                $dataIn['third_title'] = $value['booktitle'];
                $dataIn['third_time'] = $value['updatetime'];
                $dataIn['third_time_str'] = date('Y-m-d h:i:s',$value['updatetime']);
                $dataIn['update_time'] = time();
                $re = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',5)->where('third_id','=',$value['id'])->update($dataIn);
                echo '九库：ID'.$res['id'].'-'.$re.'更新成功'.PHP_EOL;
            }else{
                $dataIn['third_id'] = $value['id'];
                $dataIn['third_title'] = $value['booktitle'];
                $dataIn['third_time'] = $value['updatetime'];
                $dataIn['third_time_str'] = date('Y-m-d h:i:s',$value['updatetime']);
                $dataIn['create_time'] = date('Y-m-d h:i:s',time());
                $re = Db::connect("database.db_config_two")->name('third_booklist')->insertGetId($dataIn);
                echo '九库：'.$re.'增加成功'.PHP_EOL;
            }
        }
    }

    //书籍列表（九库）
    public function booknovel() {
        $field = ['id','third_id','third_title','siteid'];
        $siteid = 5;
        $list = Db::connect("database.db_config_two")->name('third_booklist')->field($field)->where('siteid','=',$siteid)->chunk(10,function ($datas) {
            foreach ($datas as $data){
                $result = $this->bookinfo($data['third_id']);

                if($result){
                    //处理分类id
                    $category_id = $result['category_id'];
                    $channel = 1;
                    $cateinfo = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$data['siteid'])->where('site_cid','=',$result['category_id'])->find();
                    if($cateinfo['real_cid']){
                        $category_id = $cateinfo['real_cid'];
                    }
                    if($cateinfo['name']=='女频'){
                        $channel = 2;
                    }

                    $dataIn['uid'] = 1;
                    //小说名字
                    $dataIn['title'] = $result['title'];
                    $dataIn['fullflag'] = 1; //小说进程，暂不确定
                    $dataIn['category_id'] = $category_id; //分类id
                    $dataIn['siteid'] = $data['siteid']; //内容源id
                    $dataIn['sourceid'] = $result['id']; //内容源内容id
                    $dataIn['size'] = $result['wordCount']; //字数
                    $dataIn['author'] = $result['author']; //作者
                    $dataIn['isvip'] = $result['isVip']; //是否VIP上架 0 :否  1:是
                    $dataIn['batchbuy'] = $result['isFull']; //是否整本购买
                    //$dataIn['update_time'] = $result['updatetime']; //更新时间
                    $dataIn['batchprice'] = $result['all_price']; //batchprice
                    $dataIn['coverl'] = $result['cover']; //封页图
                    $dataIn['channel'] = $channel; //频道男女
                    $dataIn['intro'] = $result['summary']; //简介

                    //判断是否存在
                    $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['id'])->find();
                    if ($res['nid'] == 0) {
                        $dataIn['create_time'] = time(); //创建时间
                        $re = Db::connect("database.db_config_two")->name('third_novel')->insertGetId($dataIn);
                        $datalist['nid'] = $re;
                        Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['id'])->update($datalist);
                        echo '九库novel：ID'.$res['id'].'-'.$re.'创建成功'.PHP_EOL;
                    }else{
                        $dataIn['update_time'] = time(); //创建时间
                        $re = Db::connect("database.db_config_two")->name('third_novel')->where('siteid','=',$data['siteid'])->where('id','=',$res['nid'])->update($dataIn);
                        echo '九库novel：'.$re.'修改成功'.PHP_EOL;
                    }

                }
            }
        });
    }


    //书籍章节列表
    public function chapters() {
        //九库分配参数
        $pid = 218;
        $sn  = md5('pid=218&key=299cc15361c629ec43d36380326dedc1');
        $url = 'http://Inf.9kus.com/communalData/chapters';

        $request = request();
        //第三站定id
        $siteid = $request->param('siteid');
        //第三方文章id
        $third_id = $request->param('third_id');
        if(!$siteid){$siteid = 5;}

        if($third_id){
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('third_id','=',$third_id)
                ->find();
        }else{
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('status','=',0)
                ->find();
        }

        if(!$result){
            return null;
        }
        //书籍id
        $bookId = $result['third_id'];
        $data = array(
            "returnType" => "JSON",
            "pid" => $pid,
            "sn" => $sn,
            "bookId" => $bookId,
        );
        $header= array();
        //通过接口获取章节信息
        $res = $this->curl_posts($url,$data,$header);
        if(!$res){return null;}
        if(($res['status']!=1)||(!$res['data'])){return null;}

        $datas = $res['data'];

        //更新书籍章节数量
        $chapters = count($datas);
        $dataUp['chapters'] = $chapters;
        Db::connect("database.db_config_two")->name('third_novel')->where('id','=',$result['id'])->update($dataUp);
        //更新书籍章节数量

        $dataIn['siteid'] = $result['siteid'];
        $dataIn['sourceid'] = $result['third_id'];
        $dataIn['status'] = 2;
        $dataIn['uid'] = 1;
        $dataIn['articleid'] = $result['id'];

        foreach ($datas as $key=>$value){
            $dataIn['sourcecid'] = $value['id'];
            $dataIn['chaptername'] = $value['title'];
            $dataIn['size'] = $value['chapterLength'];
            $dataIn['isvip'] = $value['isVip'];
            $dataIn['create_time'] = $value['updatetime'];
            $dataIn['update_time'] = $value['edittime'];
            $re = Db::connect("database.db_config_two")->name('third_chapter')
                ->where('siteid','=',$result['siteid'])
                ->where('sourceid','=',$result['third_id'])
                ->where('sourcecid','=',$value['id'])
                ->find();
            if($re){
                $r = Db::connect("database.db_config_two")->name('third_chapter')
                    ->where('siteid','=',$result['siteid'])
                    ->where('sourceid','=',$result['third_id'])
                    ->where('sourcecid','=',$value['id'])
                    ->update($dataIn);
                echo '九库third_chapter：'.$r.'修改成功'.PHP_EOL;
            }else{
                $rid = Db::connect("database.db_config_two")->name('third_chapter')->insertGetId($dataIn);
                //更新文章排序
                if($rid){
                    $dataUp['chapterorder'] = $rid;
                    Db::connect("database.db_config_two")->name('third_chapter')->where('id','=',$rid)->update($dataUp);
                }
                echo '九库third_chapter：'.$rid.'增加成功'.PHP_EOL;
            }

        }

    }


    //书籍章节列表
    public function content() {
        //九库分配参数
        $pid = 218;
        $sn  = md5('pid=218&key=299cc15361c629ec43d36380326dedc1');
        $url = 'http://Inf.9kus.com/communalData/content/';

        $request = request();
        //第三站定id
        $siteid = $request->param('siteid');
        //第三方文章id
        $third_id = $request->param('third_id');
        if(!$siteid){$siteid = 5;}

        if($third_id){
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('third_id','=',$third_id)
                ->find();
        }else{
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('status','=',0)
                ->find();
        }

        if(!$result){
            return null;
        }

        //书籍id
        $bookId = $result['third_id'];

        $datasend = array(
            "returnType" => "JSON",
            "pid" => $pid,
            "sn" => $sn,
            "bookId" => $bookId,
        );
        $header= array();

        //书籍章节
        $chapters = Db::connect("database.db_config_two")->name('third_chapter')
            ->where('siteid','=',$result['siteid'])
            ->where('sourceid','=',$result['third_id'])
            ->chunk(100,function ($datas) use($url,$datasend,$header) {
                foreach ($datas as $data){
                    $datasend['id'] = $data['sourcecid'];
                    $res = $this->curl_posts($url,$datasend,$header);
                    if(($res['status']==1)&&($res['data'])){
                        //获取文章内容
                        $dataIn['content'] =  $res['data'];
                        $re = Db::connect("database.db_config_two")->name('third_novel_content')->where('chapterid','=',$data['id'])->find();
                        if($re){
                            $r = Db::connect("database.db_config_two")->name('third_novel_content')
                                ->where('chapterid','=',$data['id'])
                                ->update($dataIn);
                            echo 'third_novel_content：'.$re['id'].'修改成功'.PHP_EOL;
                        }else{
                            $dataIn['chapterid'] = $data['id'];
                            $r = Db::connect("database.db_config_two")->name('third_novel_content')->insertGetId($dataIn);
                            echo 'third_novel_content：'.$r.'增加成功'.PHP_EOL;
                        }
                    }
                }
            });
//        //更新third_booklist表
//        if($chapters){
//            $dataUp['status'] = 1;
//            Db::connect("database.db_config_two")->name('third_booklist')->where('id','=',$result['id'])->update($dataUp);
//        }
        var_dump($chapters);
    }

    //获取书籍详情
    public function bookinfo($bookId=0) {
        if (!$bookId){
            return null;
        }
        //九库分配参数
        $pid = 218;
        $sn  = md5('pid=218&key=299cc15361c629ec43d36380326dedc1');
        $url = 'http://Inf.9kus.com/communalData/bookInfo/';

        $data = array(
            "returnType" => "JSON",
            "pid" => $pid,
            "sn" => $sn,
            "bookId" => $bookId,
        );
        $header= array();
        $result = $this->curl_posts($url,$data,$header);
        if(($result['status']!=1)&&(!$result['data'])){
            return null;
        }
        return $result['data'];

    }

    //半刻获取书籍详情
    public function bookbanke($bookId=0) {
        if (!$bookId){
            return null;
        }
        //ssid半刻分配参数
        $url = 'http://www.bankexs.com/api/share/get_book_info.php?ssid=78&aid='.$bookId;
        $header= array("content-type: application/json");
        $result = $this->curl_get($url,$header);
        //return $result;
        if(($result['code']!=200)&&(!$result['data'])){
            return null;
        }
        return $result['data'];
    }

    //依米获取书籍详情
    public function bookyimi($bookId=0) {
        if (!$bookId){
            return null;
        }
        //ssid半刻分配参数
        $url = 'http://www.bankexs.com/api/share/get_book_info.php?ssid=78&aid='.$bookId;
        $header= array("content-type: application/json");
        $result = $this->curl_get($url,$header);

        if(($result['code']!=200)&&(!$result['data'])){
            return null;
        }
        return $result['data'];
    }

    public function bankestrtotime($str) {
        $strlen = strlen($str);
        $timereturn = [];
        if($strlen==14){
            $timereturn['date'] = substr($str,0,4).'-'.substr($str,4,2).'-'.substr($str,6,2).' '.substr($str,8,2).':'.substr($str,10,2).':'.substr($str,12,2);
            $timereturn['time'] = strtotime($timereturn['date']);
        }else{
            $timereturn['time'] = time();
            $timereturn['date']  = date('Y-m-d h:i:s');
        }
        return $timereturn;
    }

    
    //书籍分类（知心）
    public function catezhixin() {
        $header= array("content-type: application/json");

        $appid = 'zL6guC';
        $url = 'http://api.zhixinbook.cn/share/category?appid='.$appid;
        //获取列表
        $result = $this->curl_get($url,$header);
        var_dump($result);
        //var_dump($result);
        //exit();
        if(($result['code']==1)&&($result['data'])) {
            $siteid = 10;
            //循环插入数据库表
            $dataIn['siteid'] = $siteid;

            $data = $result['data'];
            foreach ($data as $key=>$value){
                $name = '女频';
                if($value['channelid']==1){$name = '男频';}

                //判断是否存在分类
                $res = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$siteid)->where('site_cid','=',$value['sortid'])->find();
                if($res !=null){
                    $dataIn['name'] = $name;
                    $dataIn['title'] = $value['sortname'];
                    $dataIn['site_cid'] = $value['sortid'];
                    $dataIn['siteid'] = $siteid;
                    $re = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$siteid)->where('site_cid','=',$value['sortid'])->update($dataIn);
                    echo '知心：ID'.$res['id'].'-'.$re.'更新成功'.PHP_EOL;
                }else{
                    $dataIn['name'] = $name;
                    $dataIn['title'] = $value['sortname'];
                    $dataIn['site_cid'] = $value['sortid'];
                    $dataIn['siteid'] = $siteid;
                    $re = Db::connect("database.db_config_two")->name('third_category')->insertGetId($dataIn);
                    echo '知心：'.$re.'增加成功'.PHP_EOL;
                }

            }
        }else{
            return '';
        }

    }

    //书籍列表（知心）
    public function listzhixin() {
        $header= array("content-type: application/json");

        $appid = 'zL6guC';
        $url = 'http://api.zhixinbook.cn/share/booklist?appid='.$appid;

        //获取列表
        $result = $this->curl_get($url,$header);
        if(($result['code']==1)&&($result['data'])){
            $data = $result['data'];
            $siteid = 10;
            //循环插入数据库表
            $dataIn['siteid'] = $siteid;
            foreach ($data as $key=>$value){
                //处理返回最后更新时间
                $tirdtimestr = $value['lastupdate'];
                $tirdtime = strtotime($tirdtimestr);

                //判断是否存在
                $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$value['articleid'])->find();
                if ($res !== null) {
                    $dataIn['third_id'] = $value['articleid'];
                    $dataIn['third_title'] = $value['articlename'];
                    $dataIn['third_time'] = $tirdtime;
                    $dataIn['third_time_str'] = $tirdtimestr;
                    $dataIn['update_time'] = time();
                    $re = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$value['articleid'])->update($dataIn);
                    echo '知心：ID'.$res['id'].'-'.$re.'更新成功'.PHP_EOL;
                }else{
                    $dataIn['third_id'] = $value['articleid'];
                    $dataIn['third_title'] = $value['articlename'];
                    $dataIn['third_time'] = $tirdtime;
                    $dataIn['third_time_str'] = $tirdtimestr;
                    $dataIn['create_time'] = date('Y-m-d h:i:s',time());
                    $re = Db::connect("database.db_config_two")->name('third_booklist')->insertGetId($dataIn);
                    echo '知心：'.$re.'增加成功'.PHP_EOL;
                }
            }
        }else{
            return '书籍列表未获取到';
        }

    }

    //书籍列表-详情（知心）
    public function zhixinnovel() {
        set_time_limit(0);
        $field = ['id','third_id','third_title','siteid'];
        $siteid = 10;
        $list = Db::connect("database.db_config_two")->name('third_booklist')->field($field)->where('siteid','=',$siteid)->chunk(10,function ($datas) {
            foreach ($datas as $data){
                //$result = $this->bookbanke($data['third_id']);
                $appid = 'zL6guC';
                $url = 'http://api.zhixinbook.cn/share/bookinfo?appid=zL6guC&bookid='.$data['third_id'];
                $header= array("content-type: application/json");
                $getdata = $this->curl_get($url,$header);
                $result ='';
                if(($getdata['code']==1)&&($getdata['data'])){
                    $result =$getdata['data'];
                }

                if($result){

                    //处理分类id
                    $category_id = $result['sortid'];
                    //频道
                    $channel = 1;
                    $cateinfo = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$data['siteid'])->where('site_cid','=',$result['sortid'])->find();
                    if($cateinfo['real_cid']){
                        $category_id = $cateinfo['real_cid'];
                    }
                    if($cateinfo['name']=='女频'){
                        $channel = 2;
                    }

                    //小说名字
                    $dataIn['uid'] = 1;
                    $dataIn['title'] = $result['articlename'];
                    $dataIn['fullflag'] = 1; //小说进程，暂不确定
                    $dataIn['category_id'] = $category_id; //分类id
                    $dataIn['siteid'] = $data['siteid']; //内容源id
                    $dataIn['sourceid'] = $result['articleid']; //内容源内容id
                    $dataIn['intro']  = $result['intro']; //简介
                    $dataIn['size'] = $result['words']; //字数
                    $dataIn['author'] = $result['author']; //作者
                    $dataIn['isvip'] = 1; //是否VIP上架 0 :否  1:是
                    //$dataIn['batchbuy'] = $result['isFull']; //是否整本购买
                    //$dataIn['batchprice'] = $result['all_price']; //batchprice
                    $dataIn['coverl'] = $result['cover']; //封页图
                    $dataIn['channel'] = $channel; //性别频道
                    //小说最新更新时间？
                    $dataIn['update_time'] = strtotime($result['lastupdate']); //小说最新更新时间

                    //判断是否存在
                    $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['articleid'])->find();
                    if ($res['nid'] == 0) {
                        $dataIn['create_time'] = time(); //创建时间
                        $re = Db::connect("database.db_config_two")->name('third_novel')->insertGetId($dataIn);
                        $datalist['nid'] = $re;
                        Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['articleid'])->update($datalist);
                        echo '知心novel：ID'.$res['id'].'-'.$re.'创建成功'.PHP_EOL;
                    }else{
                        $dataIn['update_time'] = time(); //创建时间
                        $re = Db::connect("database.db_config_two")->name('third_novel')->where('siteid','=',$data['siteid'])->where('id','=',$res['nid'])->update($dataIn);
                        echo '知心novel：'.$re.'修改成功'.PHP_EOL;
                    }

                }
            }
        });

    }


    //章节章节列表-知心
    public function zhixinchapters() {
        set_time_limit(0);
        //获取所有参数
        $request = request();
        //第三站定id
        $siteid = $request->param('siteid');
        //第三方文章id
        $third_id = $request->param('third_id');
        if(!$siteid){$siteid = 10;}

        if($third_id){
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('third_id','=',$third_id)
                ->find();
        }else{
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('status','=',0)
                ->find();
        }

        //不存在返回空
        if(!$result){
            return null;
        }

        //调用接口请求书籍章节列表
        $header= array("content-type: application/json");
        $appid = 'zL6guC';
        $url = 'http://api.zhixinbook.cn/share/chapterlist?appid=zL6guC&bookid='.$result['third_id'];
        $res = $this->curl_get($url,$header);

        //不存在返回空
        if(!$res){return null;}
        if(($res['code']!=1)||(!$res['data'])){return null;}
        $datas = $res['data'];

        //更新书籍章节数量
        $chapters = count($datas);
        $dataUp['chapters'] = $chapters;
        Db::connect("database.db_config_two")->name('third_novel')->where('id','=',$result['id'])->update($dataUp);
        //更新书籍章节数量

        $dataIn['siteid'] = $result['siteid'];
        $dataIn['sourceid'] = $result['third_id'];
        $dataIn['status'] = 2;
        $dataIn['uid'] = 1;
        $dataIn['articleid'] = $result['id'];

        foreach ($datas as $key=>$value){
            $dataIn['sourcecid'] = $value['chapterid'];
            $dataIn['chaptername'] = $value['chaptername'];
            $dataIn['chapterorder'] = $value['chapterorder'];
            $dataIn['size'] = $value['words'];
            $dataIn['isvip'] = $value['isvip'];
            $dataIn['saleprice'] = $value['saleprice'];
            //创建时间
            $dataIn['create_time'] = strtotime($value['postdate']);
            //更新时间
            $dataIn['update_time'] = strtotime($value['lastupdate']);

            $re = Db::connect("database.db_config_two")->name('third_chapter')
                ->where('siteid','=',$result['siteid'])
                ->where('sourceid','=',$result['third_id'])
                ->where('sourcecid','=',$value['chapterid'])
                ->find();

            if($re){
                $r = Db::connect("database.db_config_two")->name('third_chapter')
                    ->where('siteid','=',$result['siteid'])
                    ->where('sourceid','=',$result['third_id'])
                    ->where('sourcecid','=',$value['chapterid'])
                    ->update($dataIn);
                echo '知心third_chapter：'.$r.'修改成功'.PHP_EOL;
            }else{
                $rid = Db::connect("database.db_config_two")->name('third_chapter')->insertGetId($dataIn);
                //更新文章排序
                if($rid){
                    Db::connect("database.db_config_two")->name('third_chapter')->where('id','=',$rid)->update($dataUp);
                }
                echo '知心third_chapter：'.$rid.'增加成功'.PHP_EOL;
            }

        }

    }


    //知心书籍章节内容
    public function zhixincontent() {
        //获取所有参数
        set_time_limit(0);
        $request = request();
        //第三站定id
        $siteid = $request->param('siteid');
        //第三方文章id
        $third_id = $request->param('third_id');
        if(!$siteid){$siteid = 10;}

        if($third_id){
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('third_id','=',$third_id)
                ->find();
        }else{
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('status','=',0)
                ->find();
        }

        if(!$result){
            return null;
        }

        //调用接口请求书籍章节列表
        $header= array("content-type: application/json");

        //书籍章节
        $chapters = Db::connect("database.db_config_two")->name('third_chapter')
            ->where('siteid','=',$result['siteid'])
            ->where('sourceid','=',$result['third_id'])
            ->chunk(100,function ($datas) use($header) {
                foreach ($datas as $data){
                    $url = 'http://api.zhixinbook.cn/share/chaptercontent?appid=zL6guC&bookid='.$data['sourceid'].'&chapterid='.$data['sourcecid'];
                    $res = $this->curl_get($url,$header);

                    //存在文章内容则插入
                    if(($res['code']==1)&&($res['data'])){
                        $content = $res['data']['content'];
                        //获取文章内容
                        $dataIn['content'] =  $content;
                        $re = Db::connect("database.db_config_two")->name('third_novel_content')->where('chapterid','=',$data['id'])->find();
                        if($re){
                            $r = Db::connect("database.db_config_two")->name('third_novel_content')
                                ->where('chapterid','=',$data['id'])
                                ->update($dataIn);
                            echo '知心third_novel_content：'.$re['id'].'修改成功'.PHP_EOL;
                        }else{
                            $dataIn['chapterid'] = $data['id'];
                            $r = Db::connect("database.db_config_two")->name('third_novel_content')->insertGetId($dataIn);
                            echo '知心third_novel_content：'.$r.'增加成功'.PHP_EOL;
                        }
                    }
                }
            });
      
    }
    //书籍分类（趣阅）
    // public function catequyue() {
    //     $header= array("content-type: application/json");

    //     $appid = '98';
    //     $url = 'http://quyue.api.hzquyue.com?client_id='.$appid;
    //     //获取列表
    //     $result = $this->curl_get($url,$header);
    //     var_dump($result);
    //     //var_dump($result);
    //     //exit();
    //     if(($result['code']==1)&&($result['data'])) {
    //         $siteid = 10;
    //         //循环插入数据库表
    //         $dataIn['siteid'] = $siteid;

    //         $data = $result['data'];
    //         foreach ($data as $key=>$value){
    //             $name = '女频';
    //             if($value['channelid']==1){$name = '男频';}

    //             //判断是否存在分类
    //             $res = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$siteid)->where('site_cid','=',$value['sortid'])->find();
    //             if($res !=null){
    //                 $dataIn['name'] = $name;
    //                 $dataIn['title'] = $value['sortname'];
    //                 $dataIn['site_cid'] = $value['sortid'];
    //                 $dataIn['siteid'] = $siteid;
    //                 $re = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$siteid)->where('site_cid','=',$value['sortid'])->update($dataIn);
    //                 echo '知心：ID'.$res['id'].'-'.$re.'更新成功'.PHP_EOL;
    //             }else{
    //                 $dataIn['name'] = $name;
    //                 $dataIn['title'] = $value['sortname'];
    //                 $dataIn['site_cid'] = $value['sortid'];
    //                 $dataIn['siteid'] = $siteid;
    //                 $re = Db::connect("database.db_config_two")->name('third_category')->insertGetId($dataIn);
    //                 echo '知心：'.$re.'增加成功'.PHP_EOL;
    //             }

    //         }
    //     }else{
    //         return '';
    //     }

    // }

    //书籍列表（趣阅）
    public function listquyue() {
        $header= array("content-type: application/json");

        $appid = '98';
        $url = 'http://quyue.api.hzquyue.com/index.php/common/bookList?client_id='.$appid;

        //获取列表
        $result = $this->curl_get($url,$header);
        if(empty($result['statusCode'])){
            $data = $result;
            $siteid = 11;
            //循环插入数据库表
            $dataIn['siteid'] = $siteid;
            foreach ($data as $key=>$value){
                //处理返回最后更新时间
                $tirdtimestr = date('Y-m-d H:i:s',time());
                $tirdtime = strtotime($tirdtimestr);

                //判断是否存在
                $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$value['id'])->find();
                if ($res !== null) {
                    $dataIn['third_id'] = $value['id'];
                    $dataIn['third_title'] = $value['name'];
                    $dataIn['third_time'] = $tirdtime;
                    $dataIn['third_time_str'] = $tirdtimestr;
                    $dataIn['update_time'] = time();
                    $re = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$value['id'])->update($dataIn);
                    echo '趣阅：ID'.$res['id'].'-'.$re.'更新成功'.PHP_EOL;
                }else{
                    $dataIn['third_id'] = $value['id'];
                    $dataIn['third_title'] = $value['name'];
                    $dataIn['third_time'] = $tirdtime;
                    $dataIn['third_time_str'] = $tirdtimestr;
                    $dataIn['create_time'] = date('Y-m-d h:i:s',time());
                    $re = Db::connect("database.db_config_two")->name('third_booklist')->insertGetId($dataIn);
                    echo '趣阅：'.$re.'增加成功'.PHP_EOL;
                }
            }
        }else{
            return '书籍列表未获取到';
        }

    }

    //书籍列表-详情（趣阅）
    public function quyuenovel() {
        set_time_limit(0);
        $field = ['id','third_id','third_title','siteid'];
        $siteid = 11;
        $list = Db::connect("database.db_config_two")->name('third_booklist')->field($field)->where('siteid','=',$siteid)->chunk(10,function ($datas) {
            foreach ($datas as $data){
                //$result = $this->bookbanke($data['third_id']);
                $appid = '98';
                $url = 'http://quyue.api.hzquyue.com/index.php/common/bookInfo/'.$data['third_id'].'?client_id=98';
                $header= array("content-type: application/json");
                $getdata = $this->curl_get($url,$header);
                $result ='';
                if(!empty($result['statusCode']))continue;
      
                if(empty($result['statusCode'])){
                    $result = $getdata;
                }

                if($result){

                    //处理分类id
                    $category_id = $result['category_id'];
                    //频道
                    $channel = 2;
                    // $cateinfo = Db::connect("database.db_config_two")->name('third_category')->where('title','=',$category_title)->where('site_cid','=',$result['sortid'])->find();
                    $res = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',11)->where('site_cid','=',$result['category_id'])->find();
                    if(empty($res)){
                        $dataIn['name'] = '女频';
                        $dataIn['title'] = $result['category'];
                        $dataIn['site_cid'] = $result['category_id'];
                        $dataIn['siteid'] = 11;
                        $category_id = Db::connect("database.db_config_two")->name('third_category')->insertGetId($dataIn);
                    }else{
                        $category_id = $res['id'];
                    }

                    //小说名字
                    $dataIn['uid'] = 1;
                    $dataIn['title'] = $result['name'];
                    $dataIn['fullflag'] = $result['complete_status']=='Y'?1:0; //小说进程，暂不确定
                    $dataIn['category_id'] = $category_id; //分类id
                    $dataIn['siteid'] = $data['siteid']; //内容源id
                    $dataIn['sourceid'] = $result['id']; //内容源内容id
                    $dataIn['intro']  = $result['brief']; //简介
                    $dataIn['size'] = $result['char_num']; //字数
                    $dataIn['author'] = $result['author']; //作者
                    $dataIn['isvip'] = 1; //是否VIP上架 0 :否  1:是
                    //$dataIn['batchbuy'] = $result['isFull']; //是否整本购买
                    //$dataIn['batchprice'] = $result['all_price']; //batchprice
                    $dataIn['coverl'] = $result['cover']; //封页图
                    $dataIn['channel'] = $channel; //性别频道
                    //小说最新更新时间？
                    $dataIn['update_time'] =time(); //小说最新更新时间

                    //判断是否存在
                    $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['id'])->find();
                    if ($res['nid'] == 0) {
                        $dataIn['create_time'] = time(); //创建时间
                        $re = Db::connect("database.db_config_two")->name('third_novel')->insertGetId($dataIn);
                        $datalist['nid'] = $re;
                        Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['id'])->update($datalist);
                        echo '趣阅novel：ID'.$res['id'].'-'.$re.'创建成功'.PHP_EOL;
                    }else{
                        $dataIn['update_time'] = time(); //创建时间
                        $re = Db::connect("database.db_config_two")->name('third_novel')->where('siteid','=',$data['siteid'])->where('id','=',$res['nid'])->update($dataIn);
                        echo '趣阅novel：'.$re.'修改成功'.PHP_EOL;
                    }

                }
            }
        });

    }


    //章节章节列表-趣阅
    public function quyuechapters() {
        set_time_limit(0);
        //获取所有参数
        $request = request();
        //第三站定id
        $siteid = $request->param('siteid');
        //第三方文章id
        $third_id = $request->param('third_id');
        if(!$siteid){$siteid =11;}

        if($third_id){
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('third_id','=',$third_id)
                ->find();
        }else{
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('status','=',0)
                ->find();
        }
        //不存在返回空
        if(!$result){
            return null;
        }

        //调用接口请求书籍章节列表
        $header= array("content-type: application/json");
        $appid = '98';
        $url = 'http://quyue.api.hzquyue.com/index.php/common/chapterList/'.$result['third_id'].'?client_id=98';
        $res = $this->curl_get($url,$header);

        //不存在返回空
        if(!$res){return null;}
        if(!empty($res['statusCode'])){return null;}
        $datas = $res;

        //更新书籍章节数量
        $chapters = count($datas);
        $dataUp['chapters'] = $chapters;
        Db::connect("database.db_config_two")->name('third_novel')->where('sourceid','=',$result['third_id'])->update($dataUp);
        //更新书籍章节数量
        $reslist=Db::connect("database.db_config_two")->name('third_novel')->where('sourceid','=',$result['third_id'])->find();
        $dataIn['siteid'] = $result['siteid'];
        $dataIn['sourceid'] = $result['third_id'];
        $dataIn['status'] = 2;
        $dataIn['uid'] = 1;
        
        $dataIn['articleid'] = $reslist['id'];

        foreach ($datas as $key=>$value){
            $dataIn['sourcecid'] = $value['id'];
            $dataIn['chaptername'] = $value['name'];
            $dataIn['chapterorder'] = $value['sort'];
            // $dataIn['size'] = $value['words'];
            $dataIn['isvip'] = $value['is_free']==1?0:1;
            // $dataIn['saleprice'] = $value['saleprice'];
            //创建时间
            $dataIn['create_time'] = time();
            //更新时间
            $dataIn['update_time'] = time();

            $re = Db::connect("database.db_config_two")->name('third_chapter')
                ->where('siteid','=',$result['siteid'])
                ->where('sourceid','=',$result['third_id'])
                ->where('sourcecid','=',$value['id'])
                ->find();

            if($re){
                $r = Db::connect("database.db_config_two")->name('third_chapter')
                    ->where('siteid','=',$result['siteid'])
                    ->where('sourceid','=',$result['third_id'])
                    ->where('sourcecid','=',$value['id'])
                    ->update($dataIn);
                echo '趣阅third_chapter：'.$r.'修改成功'.PHP_EOL;
            }else{
                $rid = Db::connect("database.db_config_two")->name('third_chapter')->insertGetId($dataIn);
                //更新文章排序
                if($rid){
                    Db::connect("database.db_config_two")->name('third_chapter')->where('id','=',$rid)->update($dataUp);
                }
                echo '趣阅third_chapter：'.$rid.'增加成功'.PHP_EOL;
            }

        }

    }


    //趣阅书籍章节内容
    public function quyuecontent() {
        //获取所有参数
        set_time_limit(0);
        $request = request();
        //第三站定id
        $siteid = $request->param('siteid');
        //第三方文章id
        $third_id = $request->param('third_id');
        if(!$siteid){$siteid = 11;}

        if($third_id){
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('third_id','=',$third_id)
                ->find();
        }else{
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('status','=',0)
                ->find();
        }

        if(!$result){
            return null;
        }

        //调用接口请求书籍章节列表
        $header= array("content-type: application/json");

        //书籍章节
        $chapters = Db::connect("database.db_config_two")->name('third_chapter')
            ->where('siteid','=',$result['siteid'])
            ->where('sourceid','=',$result['third_id'])
            ->chunk(100,function ($datas) use($header) {
                foreach ($datas as $data){
                    $url = "http://quyue.api.hzquyue.com/index.php/common/chapterInfo/{$data['sourceid']}/{$data['sourcecid']}?client_id=98";
                    $res = $this->curl_get($url,$header);
                    //存在文章内容则插入
                    if(empty($result['statusCode'])){
                        $content = $res['content'];
                        //获取文章内容
                        $dataIn['content'] =  $content;
                        $re = Db::connect("database.db_config_two")->name('third_novel_content')->where('chapterid','=',$data['id'])->find();
                        if($re){
                            $r = Db::connect("database.db_config_two")->name('third_novel_content')
                                ->where('chapterid','=',$data['id'])
                                ->update($dataIn);
                            echo '趣阅third_novel_content：'.$re['id'].'修改成功'.PHP_EOL;
                        }else{
                            $dataIn['chapterid'] = $data['id'];
                            $r = Db::connect("database.db_config_two")->name('third_novel_content')->insertGetId($dataIn);
                            echo '趣阅third_novel_content：'.$r.'增加成功'.PHP_EOL;
                        }
                    }
                }
            });
      
    }

    //书籍分类（点众）
    public function catedianzhong() {
        $header= array("content-type: *");

        $apikey = '9a6d5631d3b31470a26c67e627299cf7';
        $url = 'http://101.201.154.127/api/dzbook/category.html?apikey='.$apikey;

        //获取列表
        $result = $this->curl_get2($url,$header);
        if(($result['status']['code']==0)&&($result['data']['category'])) {
            $siteid = 12;
            //循环插入数据库表
            $dataIn['siteid'] = $siteid;

            $data = $result['data']['category'];
            foreach ($data as $key=>$value){
                $name = '女频';
                // if($value['channelid']==1){$name = '男频';}

                //判断是否存在分类
                $res = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$siteid)->where('site_cid','=',$value['cate_id'])->find();
                if($res !=null){
                    $dataIn['name'] = $name;
                    $dataIn['title'] = $value['cate_name'];
                    $dataIn['site_cid'] = $value['cate_id'];
                    $dataIn['siteid'] = $siteid;
                    $re = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$siteid)->where('site_cid','=',$value['cate_id'])->update($dataIn);
                    echo '点众：ID'.$res['id'].'-'.$re.'更新成功'.PHP_EOL;
                }else{
                    $dataIn['name'] = $name;
                    $dataIn['title'] = $value['cate_name'];
                    $dataIn['site_cid'] = $value['cate_id'];
                    $dataIn['siteid'] = $siteid;
                    $re = Db::connect("database.db_config_two")->name('third_category')->insertGetId($dataIn);
                    echo '点众：'.$re.'增加成功'.PHP_EOL;
                }
            }
        }else{
            return '';
        }

    }

    //书籍列表（点众）
    public function listdianzhong() {
        $header= array("content-type: application/json");

        $apikey = '9a6d5631d3b31470a26c67e627299cf7';
        $url = 'http://101.201.154.127/api/dzbook/booklist.html?apikey='.$apikey;

        //获取列表
        $result = $this->curl_get2($url,$header);
        /**
         * 一本书时返回结构
         * array(1) {
              ["item"]=>
              array(3) {
                ["id"]=>
                string(6) "115186"
                ["bookname"]=>
                string(39) "重生八零：我成了团宠娇娇女"
                ["utime"]=>
                string(19) "2023-09-02 23:28:28"
              }
            }
        */
        if(isset($result['item'])){
            $data = $result['item'];
            $siteid = 12;
            
            //单本书籍
            if(isset($data['id'])){
                //循环插入数据库表
                $dataIn['siteid'] = $siteid;
                $tirdtimestr = $data['utime'];
                $tirdtime = strtotime($tirdtimestr);
                //判断是否存在
                $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$data['id'])->find();
                if ($res !== null) {
                    $dataIn['third_id'] = $data['id'];
                    $dataIn['third_title'] = $data['bookname'];
                    $dataIn['third_time'] = $tirdtime;
                    $dataIn['third_time_str'] = $tirdtimestr;
                    $dataIn['update_time'] = time();
                    $dataIn['status'] = 1;
                    $re = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$data['id'])->update($dataIn);
                    echo '点众：ID'.$res['id'].'-'.$re.'更新成功'.PHP_EOL;
                    exit();
                }else{
                    $dataIn['third_id'] = $data['id'];
                    $dataIn['third_title'] = $data['bookname'];
                    $dataIn['third_time'] = $tirdtime;
                    $dataIn['third_time_str'] = $tirdtimestr;
                    $dataIn['create_time'] = date('Y-m-d h:i:s',time());
                    $dataIn['status'] = 1;
                    $re = Db::connect("database.db_config_two")->name('third_booklist')->insertGetId($dataIn);
                    echo '点众：'.$re.'增加成功'.PHP_EOL;
                    exit();
                }
                
            }else{
                //循环插入数据库表
                $dataIn['siteid'] = $siteid;
                foreach ($data as $key=>$value){
                    var_dump($value);
                    //处理返回最后更新时间
                    $tirdtimestr = $value['utime'];
                    $tirdtime = strtotime($tirdtimestr);
    
                    //判断是否存在
                    $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$value['id'])->find();
                    if ($res !== null) {
                        $dataIn['third_id'] = $value['id'];
                        $dataIn['third_title'] = $value['bookname'];
                        $dataIn['third_time'] = $tirdtime;
                        $dataIn['third_time_str'] = $tirdtimestr;
                        $dataIn['update_time'] = time();
                        $dataIn['status'] = 1;
                        $re = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$value['id'])->update($dataIn);
                        echo '点众：ID'.$res['id'].'-'.$re.'更新成功'.PHP_EOL;
                    }else{
                        $dataIn['third_id'] = $value['id'];
                        $dataIn['third_title'] = $value['bookname'];
                        $dataIn['third_time'] = $tirdtime;
                        $dataIn['third_time_str'] = $tirdtimestr;
                        $dataIn['create_time'] = date('Y-m-d h:i:s',time());
                        $dataIn['status'] = 1;
                        $re = Db::connect("database.db_config_two")->name('third_booklist')->insertGetId($dataIn);
                        echo '点众：'.$re.'增加成功'.PHP_EOL;
                    }
                }                
                
            }
            

        }else{
            var_dump($result);
            return '书籍列表未获取到';
        }

    }

    //书籍列表-详情（点众）
    public function dianzhongnovel() {
        set_time_limit(0);
        $field = ['id','third_id','third_title','siteid'];
        $siteid = 12;
        $request = request();
        $bookid = $request->param('third_id');
        $list = Db::connect("database.db_config_two")->name('third_booklist')->field($field)->where('siteid','=',$siteid)
        ->where('third_id','=',$bookid)->chunk(10,function ($datas) {
            $apikey = '9a6d5631d3b31470a26c67e627299cf7';
            $request = request();
            $bookid = $request->param('third_id');
            $url = 'http://101.201.154.127/api/dzbook/bookinfo.html?apikey='.$apikey.'&bookid='.$bookid;
            $header= array("content-type: application/json");
            $getdata = $this->curl_get2($url,$header);
            foreach ($datas as $data){
                $result ='';
                if($getdata){
                    $result =$getdata;
                }
                if($result){
                    //频道
                    $channel = 1;
                    $resl = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',12)->where('site_cid','=',$result['cid'])->find();
                    if(empty($resl)){
                        $dataIn['name'] = '女频';
                        $dataIn['title'] = $result['cname'];
                        $dataIn['site_cid'] = $result['cid'];
                        $dataIn['siteid'] = 12;
                        $category_id = Db::connect("database.db_config_two")->name('third_category')->insertGetId($dataIn);
                    }else{
                        $category_id = $resl['id'];
                    }
                    //小说名字
                    $dataIn['uid'] = 1;
                    $dataIn['title'] = $result['bookname'];
                    $dataIn['fullflag'] = $result['writestatus']; //小说进程，暂不确定
                    $dataIn['category_id'] = $category_id; //分类id
                    $dataIn['siteid'] = 12; //内容源id
                    $dataIn['sourceid'] = $result['bookid']; //内容源内容id
                    $dataIn['intro']  = $result['zzjs']; //简介
                    $dataIn['size'] = $result['bksize']; //字数
                    $dataIn['author'] = $result['authorname']; //作者
                    $dataIn['isvip'] = $result['license']; //是否VIP上架 0 :否  1:是
                    //$dataIn['batchbuy'] = $result['isFull']; //是否整本购买
                    $dataIn['keyword'] = $result['keyword']; //keyword
                    $dataIn['coverl'] = $result['bookpic']; //封页图
                    $dataIn['channel'] = $channel; //性别频道
                    //小说最新更新时间
                    $dataIn['update_time'] = strtotime($result['utime']); //小说最新更新时间
                    //判断是否存在
                    $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['bookid'])->find();
                    if ($res['nid'] == 0) {
                        $dataIn['create_time'] = time(); //创建时间
                        $re = Db::connect("database.db_config_two")->name('third_novel')->insertGetId($dataIn);
                        $datalist['nid'] = $re;
                        Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['bookid'])->update($datalist);
                        echo '点众novel：ID'.$res['id'].'-'.$re.'创建成功'.PHP_EOL;
                    }else{
                        $dataIn['update_time'] = time(); //创建时间
                        $re = Db::connect("database.db_config_two")->name('third_novel')->where('siteid','=',$data['siteid'])->where('id','=',$res['nid'])->update($dataIn);
                        echo '点众novel：'.$re.'修改成功'.PHP_EOL;

                    }

                }
            }
        });

    }

    //章节列表-点众
    public function dianzhongchapters() {
        set_time_limit(0);
        //获取所有参数
        $request = request();
        //第三站定id
        $siteid = $request->param('siteid');
        //调用接口请求书籍章节列表
        $header= array("content-type: application/json");
        $apikey = '9a6d5631d3b31470a26c67e627299cf7';
        //第三方文章id
        $bookid=$third_id = $request->param('third_id');

        $url = 'http://101.201.154.127/api/dzbook/chapterlist.html?apikey='.$apikey.'&bookid='.$third_id;
        $res = $this->curl_get2($url,$header);
        
        if(!$siteid){$siteid = 12;}

        if($third_id){
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('third_id','=',$third_id)
                ->find();
        }else{
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('status','=',0)
                ->find();
        }

        //不存在返回空
        if(!$result){
            return null;
        }

        //不存在返回空
        if(!$res){return null;}
        if((!$res['vol']['chapter'])){return null;}
        $datas = $res['vol']['chapter'];
        //更新书籍章节数量
        $chapters = count($datas);
        $dataUp['chapters'] = $chapters;
        Db::connect("database.db_config_two")->name('third_novel')->where('sourceid','=',$result['third_id'])->update($dataUp);
        //更新书籍章节数量
        $reslist=Db::connect("database.db_config_two")->name('third_novel')->where('sourceid','=',$result['third_id'])->find();
        $dataIn['siteid'] = $siteid;
        $dataIn['sourceid'] = $bookid;
        $dataIn['status'] = 2;
        $dataIn['uid'] = 1;
        $dataIn['articleid'] = $reslist['id'];

        foreach ($datas as $key=>$value){
            $dataIn['sourcecid'] = $value['chapterid'];
            $dataIn['chaptername'] = $value['chaptername'];
            $dataIn['chapterorder'] = $value['order'];
            $dataIn['size'] = $value['word_num'];
            $dataIn['isvip'] = $value['license'];
            $dataIn['saleprice'] = 0;
            //创建时间
            $dataIn['create_time'] = strtotime($value['utime']);
            //更新时间
            $dataIn['update_time'] = strtotime($value['utime']);

            $re = Db::connect("database.db_config_two")->name('third_chapter')
                ->where('siteid','=',$siteid)
                ->where('sourceid','=',$bookid)
                ->where('sourcecid','=',$value['chapterid'])
                ->find();
            if($re){
                $r = Db::connect("database.db_config_two")->name('third_chapter')
                    ->where('siteid','=',$siteid)
                    ->where('sourceid','=',$bookid)
                    ->where('sourcecid','=',$value['chapterid'])
                    ->update($dataIn);
                echo '点众third_chapter：'.$r.'修改成功'.PHP_EOL;
            }else{
                $rid = Db::connect("database.db_config_two")->name('third_chapter')->insertGetId($dataIn);
                //更新文章排序
                if($rid){
                    Db::connect("database.db_config_two")->name('third_chapter')->where('id','=',$rid)->update($dataUp);
                }
                echo '点众third_chapter：'.$rid.'增加成功'.PHP_EOL;
            }
        }
    }

    //点众书籍章节内容
    public function dianzhongcontent() {
        set_time_limit(0);

        //获取所有参数
        $request = request();
        //第三站定id
        $siteid = $request->param('siteid');
        //第三方文章id
        $third_id = $request->param('third_id');
        if(!$siteid){$siteid = 12;}
                                                                                    
        if($third_id){
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('third_id','=',$third_id)
                ->find();
        }else{
            $result = Db::connect("database.db_config_two")->name('third_booklist')
                ->where('siteid','=',$siteid)
                ->where('status','=',0)
                ->find();
        }

        if(!$result){
            return null;
        }
        
        //调用接口请求书籍章节列表
        $header= array("content-type: application/json");
        //书籍章节
        Db::connect("database.db_config_two")->name('third_chapter')
            ->where('siteid','=',$siteid)
            ->where('sourceid','=',$result['third_id'])
            ->chunk(100,function ($datas) use($header) {
                foreach ($datas as $data){
                    $apikey = '9a6d5631d3b31470a26c67e627299cf7';
                    $url = 'http://101.201.154.127/api/dzbook/chapterContent.html?apikey='.$apikey.'&bookid='.$data['sourceid'].'&chapterid='.$data['sourcecid'];
                    $res = $this->curl_get2($url,$header);
                    //存在文章内容则插入
                    if($res){
                        $content = $res[0];
                        //获取文章内容
                        $dataIn['content'] =  $content;
                        $re = Db::connect("database.db_config_two")->name('third_novel_content')->where('chapterid','=',$data['id'])->find();
                        if($re){
                            $r = Db::connect("database.db_config_two")->name('third_novel_content')
                                ->where('chapterid','=',$data['id'])
                                ->update($dataIn);
                            echo '点众third_novel_content：'.$re['id'].'修改成功'.PHP_EOL;
                        }else{
                            $dataIn['chapterid'] = $data['id'];
                            $r = Db::connect("database.db_config_two")->name('third_novel_content')->insertGetId($dataIn);
                            echo '点众third_novel_content：'.$r.'增加成功'.PHP_EOL;
                        }
                    }
                }
            });
    }

    // 溪文
   
    public function getApiAndSign($uri, $book_id='', $chapter_id='')
    {
        $key = '72AAF630A0B517A05F59B03C4CC94039';
        $appsecret = '72AAF630A0B517A05F59B03C4CC94039016c571bfd0998d32351f6fa5427cb1062bbd8d5';

        $sign = md5($appsecret . $book_id . $chapter_id );
        $uri  = str_replace(array("{{book_id}}","{{chapter_id}}","{{siteid10_sign}}","{{siteid10_key}}"),array($book_id, $chapter_id, $sign, $key), $uri);
        return $uri;
    }
    public function category( $channelid, $sourceCid, $sourceName, $siteid)
    {
        $name = '女频';
        if($channelid==1){$name = '男频';}

        //判断是否存在分类
        $res = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$siteid)->where('site_cid','=',$sourceCid)->find();
        if($res !=null){
            $dataIn['name'] = $name;
            $dataIn['title'] = $sourceName??'未知';
            $dataIn['site_cid'] = $sourceCid;
            $dataIn['siteid'] = $siteid;
            $re = Db::connect("database.db_config_two")->name('third_category')->where('siteid','=',$siteid)->where('site_cid','=',$sourceCid)->update($dataIn);
            return $res['id'];
        }else{
            $dataIn['name'] = $name;
            $dataIn['title'] = $sourceName??'未知';
            $dataIn['site_cid'] = $sourceCid;
            $dataIn['siteid'] = $siteid;
            $re = Db::connect("database.db_config_two")->name('third_category')->insertGetId($dataIn);
            return $re;
        }
    }

    //书籍列表（溪文）
    public function listxiwen($nid=0) {
        $header= array("content-type: application/json");
        $siteid = 3;
        $start_time = microtime(true); 
        $url = $this->getApiAndSign('http://xyopen.8kana.com/channel/books?key={{siteid10_key}}&sign={{siteid10_sign}}');

        //获取列表
        $response = $this->curl_get($url,$header);
        if($response['Code'] == 0){
            $data = $response['Data'];
            $dataIn = [];
            //循环插入数据库表
            $dataIn['siteid'] = $siteid;
            $i = 0;
            foreach ($data as $key=>$value){
                //只抓取一本小说

                // echo count($data)."/".$i."\t";
                //判断是否存在
                $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$value['id'])->find();
                if ($res !== null) {
                    $dataIn['third_id'] = $value['id'];
                    $dataIn['third_title'] = $value['name'];
                    $dataIn['update_time'] = time();
                    $re = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$siteid)->where('third_id','=',$value['id'])->update($dataIn);
                    // echo 'siteid='.$siteid.'：ID'.$res['id'].'-'.$re.'更新成功'.PHP_EOL;
                }else{
                    $dataIn['third_id'] = $value['id'];
                    $dataIn['third_title'] = $value['name'];
                    $dataIn['create_time'] = date('Y-m-d h:i:s',time());
                    $re = Db::connect("database.db_config_two")->name('third_booklist')->insertGetId($dataIn);
                    // echo 'siteid='.$siteid.'：'.$re.'增加成功'.PHP_EOL;
                }
                ++$i;
            }
            echo "总小说本数: ".count($data)."\t";
        }else{
            echo  $siteid."\t".$response['Msg']."\n";
        }
        $end_time = microtime(true); 
        echo "\t拉小说列表end， 执行时间 =".($end_time - $start_time)."秒 \n";

    }

    //书籍列表-详情（溪文）
    public function xiwennovel($nid=0) {
        set_time_limit(0);
        //获取所有参数
        $request = request();
        //第三站定id
        $nid = $request->param('nid');
        $start_time = microtime(true); 
        $field = ['id','third_id','third_title','siteid','nid'];
        $siteid = 3;
        $map = "";
        $map = 'nid='. $nid;
     
        
        $limit = 0;
        $pagesize = 200;
        $total = Db::connect("database.db_config_two")->name('third_booklist')->field($field)->where( 'siteid='.$siteid )->where($map)->count();
        $total2 = $total;
        $list = Db::connect("database.db_config_two")->name('third_booklist')->field($field)->where( 'siteid='.$siteid )->where($map)->chunk($pagesize ,function ($datas) use($total,  $total2, $siteid) {
            $i = 0;
            foreach ($datas as $data){
                
                echo "进度".$total."/".$total2."\t";
                if($data['nid'] > 0 ){
                    $nid = $data['nid'];
                }else{
       
                    $api = $this->getApiAndSign('http://xyopen.8kana.com/channel/books?book_id={{book_id}}&key={{siteid10_key}}&sign={{siteid10_sign}}',$book_id=$data['third_id']);
                    $response = $this->curl_get($api);
                    if($response['Code'] == 0){
                        $result = $response['Data'];
                        $dataIn = [];
                        $dataIn['uid'] = 1;
                        $dataIn['title'] = $result['name']??'未知';
                        $dataIn['fullflag'] = $result['is_serial'] == 1 ? 0 : 1; //小说进程，暂不确定
                        $dataIn['category_id'] = $this->category( 1, $result['class_id'], $result['class_name'], $siteid); //分类id
                        // dump($dataIn);exit;
                        $dataIn['siteid'] = $data['siteid']; //内容源id
                        $dataIn['sourceid'] = $result['id']; //内容源内容id
                        $dataIn['intro']  = $result['intro']; //简介
                        // $dataIn['size'] = $result['size']; //字数
                        $dataIn['author'] = $result['author']; //作者
                        $dataIn['isvip'] = $result['is_vip']; //是否VIP上架 0 :否  1:是
                        //$dataIn['batchbuy'] = $result['isFull']; //是否整本购买
                        //$dataIn['batchprice'] = $result['all_price']; //batchprice
                        $dataIn['coverl'] = $result['cover']; //封页图
                        $dataIn['channel'] = 1; //性别频道
                        //小说最新更新时间？
                        $dataIn['update_time'] = strtotime($result['update_time']); //小说最新更新时间
                        //判断是否存在
                        $res = Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['id'])->find();
                        if ($res['nid'] == 0) {
                            $dataIn['create_time'] = time(); //创建时间
                            $re = Db::connect("database.db_config_two")->name('third_novel')->insertGetId($dataIn);
                            $nid = $re;
                            $datalist['nid'] = $re;
                            Db::connect("database.db_config_two")->name('third_booklist')->where('siteid','=',$data['siteid'])->where('third_id','=',$result['id'])->update($datalist);
                            // echo 'siteid='.$data['siteid'].'：ID'.$res['id'].'-'.$re.'创建成功'.PHP_EOL;
                        }else{
                            $dataIn['update_time'] = time(); //创建时间
                            // dump($dataIn);exit;
                            $re = Db::connect("database.db_config_two")->name('third_novel')->where('siteid','=',$data['siteid'])->where('id','=',$res['nid'])->update($dataIn);
                            $nid = $res['nid'];
                            // echo 'siteid='.$data['siteid'].'：'.$re.'修改成功'.PHP_EOL;
                        }
                    }else{
                        echo  $data['siteid']."\t".$data['third_id']."\t".$response['Msg']."\n";
                        continue;
                    }
                }
                $this->xiwenchapters($siteid, $nid);
                $this->xiwencontent($siteid, $nid);
                $total2--;
                ++$i;
            }
        });

        $end_time = microtime(true); 
        echo "拉小说详情end 执行时间 =".($end_time - $start_time)."秒 \n";

    }

    //章节列表-溪文
    public function xiwenchapters($siteid, $nid=0) {
        $start_time = microtime(true); 
        set_time_limit(0);
        //获取所有参数
        $field = ['id','third_id','third_title','siteid','nid'];
        $map = "";
        if($nid > 0 ){
            $map = 'nid='. $nid;
        }
        $map2 = [];
        $list = Db::connect("database.db_config_two")->name('third_booklist')->field($field)->where( 'siteid='.$siteid )->where($map)->where($map2)->chunk(10,function ($booklist) {
            $i = 0;

            foreach ($booklist as $result){
                $siteid = $result['siteid'];
                $api = $this->getApiAndSign('http://xyopen.8kana.com/channel/chapters?book_id={{book_id}}&key={{siteid10_key}}&sign={{siteid10_sign}}',$book_id=$result['third_id']);
                $response = $this->curl_get($api);

                if($response['Code'] == 0){
                    $datas = $response['Data'];
                    //更新书籍章节数量
                    $chapters = count($datas);
                    $dataUp['chapters'] = $chapters;
                    Db::connect("database.db_config_two")->name('third_novel')->where('id','=',$result['nid'])->update($dataUp);
                    //更新书籍章节数量
            
                    $dataIn['siteid'] = $result['siteid'];
                    $dataIn['sourceid'] = $result['third_id'];
                    $dataIn['status'] = 2;
                    $dataIn['uid'] = 1;
                    $dataIn['articleid'] = $result['nid'];
                    $count = count($datas);
                    $i = 1;
                    foreach ($datas as $key=>$value){
                        $dataIn['sourcecid'] = $value['id'];
                        $dataIn['chaptername'] = $value['title']??'未知';
                        // $dataIn['size'] = 0;
                        $dataIn['isvip'] = 0;
                        $dataIn['saleprice'] = 0;
                        $dataIn['chapterorder'] =$i;
                        //创建时间
                        $dataIn['create_time'] = strtotime($value['update_time']);
                        //更新时间
                        $dataIn['update_time'] = strtotime($value['update_time']);
            
                        $re = Db::connect("database.db_config_two")->name('third_chapter')
                            ->where('siteid','=',$result['siteid'])
                            ->where('sourceid','=',$result['third_id'])
                            ->where('sourcecid','=',$value['id'])
                            ->find();
            
                        if($re){
                            $r = Db::connect("database.db_config_two")->name('third_chapter')
                                ->where('siteid','=',$result['siteid'])
                                ->where('sourceid','=',$result['third_id'])
                                ->where('sourcecid','=',$value['id'])
                                ->update($dataIn);
                            // echo 'siteid'.$siteid.'third_chapter：'.$r.'修改成功'.PHP_EOL;
                            $msg = "修改";
                        }else{
                            $rid = Db::connect("database.db_config_two")->name('third_chapter')->insertGetId($dataIn);
                            //更新文章排序
                            if($rid){
                                // $dataUp['chapterorder'] = $rid;
                                Db::connect("database.db_config_two")->name('third_chapter')->where('id','=',$rid)->update($dataUp);
                            }
                            // echo 'siteid'.$siteid.'third_chapter：'.$rid.'增加成功'.PHP_EOL;
                            $msg = "添加";
                        }
                        $i++;
                        
            
                    }
                    echo "总". $count."章节 \t";
                }
                ++$i;
            }
            
        });
        $end_time = microtime(true); 
        echo "拉小说章节列表end 执行时间 =".($end_time - $start_time)."秒 \n";
    }

    //溪文书籍章节内容
    public function xiwencontent($siteid, $nid=0) {
        set_time_limit(0);
        $start_time = microtime(true); 
        echo "\t\t拉小说章节内容start \t";
        $map = "";
        if($nid > 0 ){
            $map = 'nid='. $nid;
        }
        $map2 = [];
        
        $field = ['id','third_id','third_title','siteid','nid','status'];
        $list = Db::connect("database.db_config_two")->name('third_booklist')->field($field)->where( 'siteid='.$siteid )->where($map)->where($map2)->chunk(10,function ($booklists) use($siteid) {
            foreach ($booklists as $key => $booklist) {
                
                    $chapters = Db::connect("database.db_config_two")->name('third_chapter')
                    ->where('siteid','=',$booklist['siteid'])
                    ->where('sourceid','=',$booklist['third_id'])
                    ->chunk(100,function ($datas)  {
                        foreach ($datas as $data){
                            $api = $this->getApiAndSign('http://xyopen.8kana.com/channel/chapters?book_id={{book_id}}&chapter_id={{chapter_id}}&key={{siteid10_key}}&sign={{siteid10_sign}}', $book_id=$data['sourceid'], $chapter_id=$data['sourcecid']);
                            $response = $this->curl_get($api);
                            //存在文章内容则插入
                            if($response['Code'] == 0){
                                $content = $response['Data']['content'];
                                $content = str_replace("</p><p>","\n", $content);
                                $content = str_replace("
                                ","", $content);
                                //获取文章内容
                                $dataIn['content'] =  $content;
                                $re = Db::connect("database.db_config_two")->name('third_novel_content')->where('chapterid','=',$data['id'])->find();
                                if($re){
                                    $r = Db::connect("database.db_config_two")->name('third_novel_content')
                                        ->where('chapterid','=',$data['id'])
                                        ->update($dataIn);
                                    // echo "siteid=".$data['siteid'].' third_novel_content：'.$re['id'].'修改成功'.PHP_EOL;
                                }else{
                                    $dataIn['chapterid'] = $data['id'];
                                    $r = Db::connect("database.db_config_two")->name('third_novel_content')->insertGetId($dataIn);
                                    // echo "siteid=".$data['siteid'].' third_novel_content：'.$r.'增加成功'.PHP_EOL;
                                }
                                if($data['size'] == 0){
                                    $size =  mb_strlen($content, "UTF-8");
                                    $datachapter = [];
                                    $datachapter['size'] = get_count_content($content);
                                    $datachapter['chaptername'] = $response['Data']['title']?? '未知';
                                    $datachapter['isvip'] = $response['Data']['is_vip'];
                                    $r = Db::connect("database.db_config_two")->name('third_chapter')
                                        ->where('id','=',$data['id'])
                                        ->update($datachapter);
                                    if($r){
                                        // echo "siteid=".$data['siteid'].' third_chapter：'.$re['id'].'修改成功'.PHP_EOL;
                                    }else{
                                        // echo "siteid=".$data['siteid'].' third_chapter：'.$re['id'].'修改成功'.PHP_EOL;
                                    }
                                }
                            }
                        }
                    });
                    Db::connect("database.db_config_two")->name('third_booklist')->where( 'siteid='.$siteid )->where("id=".$booklist['id'])->update(['status' => 1]);

                    $size = Db::connect("database.db_config_two")->name('third_chapter')
                    ->where('siteid','=',$booklist['siteid'])
                    ->where('sourceid','=',$booklist['third_id'])
                    ->sum('size');
                    Db::connect("database.db_config_two")->name('third_novel')
                    ->where('siteid','=',$booklist['siteid'])
                    ->where('sourceid','=',$booklist['third_id'])->update(['size' => $size]);
                    $size = 0;
                   
                $chaptersCount =  Db::connect("database.db_config_two")->name('third_chapter')
                ->where('siteid','=',$booklist['siteid'])
                ->where('sourceid','=',$booklist['third_id'])->count();
                echo $chaptersCount." 章节内容\t";
            }
        });

        $end_time = microtime(true); 
        echo "拉小说章节内容end 执行时间 =".($end_time - $start_time)."秒 \n";
    }

    public function curl_posts($url,$data,$header=array()){
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url);
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        if($header){
            curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS,$data);
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false);
        $returns = curl_exec ( $ch );
        curl_close ( $ch );
        $result = json_decode($returns,true);
        return $result;
    }

    public function curl_get($url,$header=false){
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url);
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        if($header){
            curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false);
        $returns = curl_exec ( $ch );
        curl_close ( $ch );
        $result = json_decode($returns,true);
        return $result;
    }
    public function curl_get2($url,$header=false){
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url);
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        if($header){
            curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false);
        $returns = curl_exec ( $ch );
        curl_close ( $ch );
        $result=[];
        if($returns){
            $result = json_decode(json_encode(@simplexml_load_string($returns, 'SimpleXMLElement', LIBXML_NOCDATA)),1);
        }
        return $result;
    }


}